Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual C++ 2008 express problem with run and output 1

Status
Not open for further replies.

freddy5430

Programmer
Jun 21, 2008
5
IE
my message is in 3 sections:

1. the C++ code is:

/* This program enciphers files using one of Vigenere, Beauford or
Variant Beauford ciphers. Note inverses means this code also
decrypts - eg encipherment by Vigenere can be deciphered by
enciphering with Variant Beauford with the same key. Similarly,
Beauford is its own inverse.

Input via file or keyboard is of the following format:
cipher-type
key
the message in free format, of any desired length
Output will be of the form:
theen ciphe redme ssage

The cipher type is given by the single letter V (or v) for Vigenere,
B (or b) for Beauford and A (or a) for Variant Beauford. Should a
different letter be used, a message will be printed to the screen and
Vigenere used.

Currently the maximum allowed key is 10 characters. Excess is discarded
with a warning message sent to the screen. MAXKEYLENGTH can be changed
for longer or shorter limits. The key can be *any* ASCII characters, not
just alphabetics.

Note upper and lower case plaintext are enciphered by converting all to
lower case first, with the output solely lower case. Non-alphabetics are
discarded, however relatively minor adjustments could be made to allow
longer alphabets.

The output is given in the traditional 5 letter chunks, but BLOCKLENGTH
can be changed to suit personal taste. Similarly, 80 character lines are
assumed, but other lengths can be set with LINELENGTH.

Two versions of main are given: one for input from the keyboard or from
file redirection, the other for input from a file named on the command
line. Just uncomment which you wish to use. Note both send output to the
screen by default, and allow output redirection to a file (eg ... > outfile)
If you wish to specify the output file, the modification is minor.

An example for program verification:
V
orbit
An ambassador is an Honest man sent to Lie and Intrigue abroad for
the benefit of his Country
produces output:
oebuu ojtiw cijat bypvx gknig gvobm ccjmt bujvm fzhcx osswt rwpzm
vvcmg swjbh tyjav clobk m

Note no padding at the end to hide the message length: this is easily added.
For instance, the final message letter can be enciphered repeatedly (still
with progression through the key to hide the effect) until a block is full.

Author: Dr Leisa Condie, December 1992.
phoenix@neumann.une.edu.au
Dept of Mathematics, Statistics and Computing Science,
University of New England - Armidale,
New South Wales, 2351, AUSTRALIA.
*/

#define ALPHA 26 /* length of alphabet for modulo */
#define MAXKEYLENGTH 10 /* maximum length of the key used */
#define BLOCKLENGTH 5 /* for output, how many chars in a block */
#define LINELENGTH 80 /* maximum output characters per line */

char key[MAXKEYLENGTH+1]; /* encipherment key: +1 for possible newline */
int blockcount = 0; /* counts of chars in printed block */
int linechars = 0; /* count of chars printed on current line */
int keylength = 0; /* holds actual length of the key */
int vigenere=0,beauford=0,varbeau=0;
/* cipher type is set to 1 (TRUE) if chosen */
FILE *fp; /* set to stdin if interactive, else file */

void getsetup(void)
{
char ch; /* generic character variable */
char *tmp = key; /* pointer to key array */

/* find cipher type */
ch = getc(fp);
if (ch == 'V' || ch == 'v') vigenere =1;
else if (ch == 'B'|| ch == 'b') beauford =1;
else if (ch == 'A'|| ch == 'a') varbeau =1;
else { /* otherwise error, so notify by stderr and use Vigenere */
fprintf(stderr,"V/B/A ciphers only - Vigenere assumed\n");
vigenere =1;
}

while ((ch=getc(fp)) != '\n'); /* if extraneous input, clear it! */


/* get key - anything after the MAXKEYLENGTH'th char is discarded */

for (keylength=0; keylength < MAXKEYLENGTH; keylength++)
if ((key[keylength]= getc(fp)) == '\n') break;

if (key[keylength] != '\n'){
while ((ch=getc(fp)) != '\n'); /* if excess key, clear it! */
fprintf(stderr,"Key truncated to %d characters\n",keylength);
}
}

int encipher(int i)
{
/* Takes argument i - where we are in the key,
Returns tmp - the ciphertext equivalent of the input if
the input was alphabetic, else the input character unchanged */
char ch; /* character read in */
int tmp; /* for cipher char calculation */

ch = getc(fp);
if (ch >= 'A' && ch <= 'Z') { /* convert to lowercase */
ch = ch - 'A' + 'a'; /* don't trust tolower() */
}
if (ch >= 'a' && ch <= 'z') { /* encipher */
if (vigenere)
tmp = (ch + key - 2*'a') % ALPHA;
else if (beauford)
tmp = (key - ch) % ALPHA;
else tmp = (ch - key) % ALPHA;

/* make offset positive and convert to lowercase char */
while (tmp < 0) tmp += ALPHA;
tmp += 'a';
}
else tmp = ch; /* else return character unchanged */
return(tmp);
}

void outputcipher(void)
{
int cipherch, i=0; /* cipher character */

while (!feof(fp)) { /* keep going whilst there is input */
cipherch = encipher(i); /* generate cipher character */
if (cipherch < 'a' || cipherch > 'z') /* invalid char in */
continue; /* ignore code below - restart loop */

/* check we haven't finished key and need to restart it */
if (i == keylength-1) i=0;
else i++;

/* if a BLOCKLENGTH block is finished print a space */
if (blockcount == BLOCKLENGTH) {
/* check whether a newline is needed yet */
if (linechars > LINELENGTH - BLOCKLENGTH) {
putchar('\n');
linechars = 0;
}
else {
putchar(' ');
linechars++;
}
blockcount = 0;
}
/* print enciphered character */
putchar(cipherch);
blockcount++;
linechars++;
}
putchar('\n');
}

/* This version of main is set for input to come from the keyboard either
directly or through file redirection: e.g. program < input_file
*/
/*
void main(void)
{
fp = stdin;
getsetup();
outputcipher();
}
*/

/* This version of main looks for an input file whose name is specified on
the argument line: e.g. program input_file
*/
void main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr,"Usage: program <input_file>\n");
exit(1);
}

if ((fp = fopen(argv[1],"r")) == NULL) {
fprintf(stderr,"File %s cannot be read from\n",argv[1]);
exit(1);
}

getsetup();
outputcipher();
}

2.main() function is expecting command line parameters. In order to pass command line parameters,I pass them in through visual studio, using Project Properties > Debugging > Command Parameters.
Then run using F5 in Visual Studio, the program opens in the debugger, and without a breakpoint, the console window closes as soon as the program finishes execution. To keep the console window open,I ran without the debugger (Ctrl+F5), put a breakpoint at the end of main, add some line like cin.getline() at the end of main.
This did not work it gave 8 errors.
In another run the code was not listed in the ide, just a pointer to it.this builds ok and gives a log,but there is no break to input a line and there is no output.

Pleaser help.


2.





 
1. Use CODE tags for snippets (see Process TGML link on the form below).
2. Don't forget to open input file (in main) before reading (in getsetup).
3. Use standard int main (not void main)...
 
I have done this but I cant put input into prog or get output from it.
 
Let's start from the beginnig:
1. Are you able to compile your source file and build your console application w/o error diagnostic messages?
2. Have you proper includes for this code?
Code:
#include <stdio.h>
#include <stdlib.h>
?

I have input/output for this code w/o any problems (VS 2008 Express)...
 
Hi,This is the start of my code please look at it and let me know if it is wrong.
Please refer to the whole code sent previously to see how it continues.
Your 2 replies seem to tell me that my code is wrong, but how can I correct it?
Thanks,
Freddy.

============================================================
#define ALPHA 26 /*

length of alphabet for modulo */
#define MAXKEYLENGTH 10 /*

maximum length of the key used */
#define BLOCKLENGTH 5 /* for

output, how many chars in a block */
#define LINELENGTH 80 /*

maximum output characters per line */

char key[MAXKEYLENGTH+1]; /*

encipherment key: +1 for possible

newline */
int blockcount = 0; /*

counts of chars in printed block */
int linechars = 0; /*

count of chars printed on current line

*/
int keylength = 0; /*

holds actual length of the key */
int vigenere=0,beauford=0,varbeau=0;
/*

cipher type is set to 1 (TRUE) if

chosen */
FILE *fp; /* set

to stdin if interactive, else file */

void getsetup(void)
{
char ch; /*

generic character variable */
char *tmp = key; /*

pointer to key array */

/* find cipher type */
ch = getc(fp);
if (ch == 'V' || ch == 'v')

vigenere =1;
===========================================================

etc.
 
See answer #1 in my previous post. Where is the question?

See advice #2 in my previous post. Add these includes in the code.

Again and again: I have built the solution from the initial post code (Visual Studio 2008 Express, build solution from the existing code). It works.
 
I will try and ans. your q's:
==========================================================
Ur reply 1:
"open input file (in main) before reading (in getsetup)"

my code has:
"void getsetup(void)" at about line 11 of the 2nd code snippet I sent.

Should I put "int main ()" in the line above it to open the input file?

So we have:
int main ()
void getsetup(void) ?
==========================================================
2. U:
"compile your source file and build your console application w/o error diagnostic messages?"

ME:
The folder I use is:
" my computer"/programs/ vis studio/vc/vc wizards" this has a compiler and this builds codes as a console ok with a pre-defined header and a pointer to the location of the codes in my docs,
but when i run debug it is ok BUT there is no input or output.
There are NO ERRORS at this stage.

Pressing F5 is supposed to run the prog and get input and output, but it just repeats the process.

It may be that the prog is supposed to run from another folder but I dont know which one.
There are 37 folders and 39 "clickers"!

What is "Common 7" for?

The debug run gives:
'vig7.exe': Loaded 'C:\Documents and Settings\Brian Southey\My Documents\Maths and computing\Cryptography\Vigenere\vig7\vig7\Debug\vig7.exe', Symbols loaded.
'vig7.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'vig7.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'vig7.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll'
The program '[2200] vig7.exe: Native' has exited with code 0 (0x0).
But where are these files?
Can they be run from one of these files?

==========================================================
3. U: "Have you proper includes for this code?
#include <stdio.h>
#include <stdlib.h>"

Me: No.

The code starts with "#define ALPHA"........
Then "char key..."
"int blockcount = 0"......
"FILE *fp; /* set to stdin......
"void getsetup(void)"......

Where would i put the std includes?

========================================================
3. As I said the actual code does not appear on the platform.
If I paste the full code onto the platform with or without the pre-defined header and build it I get many errors.
===========================================================
Please help,
Freddy.




 
What's a knotty question...
Suppose we have a source file with contents from your original post. Let its name is vig.c (it is C language source;). Make a new directory and place this file in this directory. Now start VS 2008...

Select File|New|Procect From Existing Code. Say to VS Wizard this directory name, select Win32 Console Application. Now you have a project with vig.c as a source file.

Add two includes (see my posts above) at the start of the source. Try to build solution w/o errors (may be with warnings). Now you have vig.exe (in debug mode by default).

You must define command line argument with processed file name (path). Open Project|Properties window, select Debug page and type the path in Command Argument field. Now you may start the application (for example, via F5 or better Ctrl-F5) and enjoy its output in console output window...

Sorry, I do not understand what's your pro level in VS 2008 and C language. If you want to run this program you must define input file name as the 1st (one and only one;) atgument then the program prints its output via stdout in console window...

Apropos, this subforum (Win API) is dedicated to pecularities of Windows API functions calls, not to standard Visual Studio operations with ordinary C files...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top