...

View Full Version : Skipping problem in C



boris
05-01-2004, 04:44 AM
I've made a program to encrypt and decrypt messages for my uni course but I'm having trouble in that my program seems to skip a scanf statement.
In my code it's the third one within the input function. i tried deleting the second scanf statement in that function and it works, but i need that statement in there too. its like theres a limit on the amount of scanf statements you can have in a function. also, the isValid function has an assertion error in it probably when it calls isaplha. what can i do about that? Other than those problems the program runs fine. Could it be a compiler problem? I use Visual Studio .NET.

Try to excuse my poor coding, I only started this year with C.

// this program allows the user to encrypt and decrypt messages

// exit codes: (1) this means the user has tried to manipulate a non alphabet character

#include <stdafx.h> // this is normally <stdio.h> but Visual Studio uses this header
#include <stdlib.h>
#include <ctype.h>

#define STRINGMAX 100

char encrypt(char ch, int *keyPtr);
int input(char array[STRINGMAX], int *keyPtr);
char decrypt(char ch, int *keyPtr);
void output(char array[STRINGMAX], int length);
int isValid(char ch);

int main()
{
char array[STRINGMAX];
int key, *keyPtr, n;

keyPtr = &key;

if(input(array, keyPtr) == 1)
{
output(array, 100); // debugging statement
for(n = 0; n < STRINGMAX; n++)
{
if(array[n] != EOF)
{
if(isValid(array[n]) == 0)
{
array[n] = encrypt(array[n], keyPtr);
}
else
{
exit(1);
}
}
else
{
break;
}
}
}
else
{
for(n = 0; n < STRINGMAX; n++)
{
if(array[n] != EOF)
{
if(isValid(array[n]) == 0)
{
array[n] = decrypt(array[n], keyPtr);
}
else
{
exit(1);
}
}
else
{
break;
}
}
}
output(array, n);

system("pause");

return 0;
}

char encrypt(char ch, int *keyPtr)
{
if(ch != 32)
{
ch += *keyPtr;
}

if(ch > 90 && ch < 97)
{
ch += 6;
}
else if(ch > 122)
{
ch -= 58;
}

return ch;
}

int input(char array[STRINGMAX], int *keyPtr)
{
char mode = 'e';

printf("Enter the key: ");
scanf("%d/n", keyPtr);
printf("Enter the mode (e = encrypt, d = decrypt): ");
scanf("%c/n", &mode);
printf("Enter the string to manipulate (max %d characters): \n", STRINGMAX);
scanf("%s/n", &array);
if(mode == 'e')
{
return 1;
}
else if(mode == 'd')
{
return 0;
}
}

char decrypt(char ch, int *keyPtr)
{
if(ch != 32)
{
ch += *keyPtr;
}

if(ch > 90 && ch < 97)
{
ch -= 6;
}
else if(ch < 65 && ch != 32)
{
ch += 58;
}

return ch;
}

void output(char array[STRINGMAX], int length)
{
int errors = 0;

for(int n = 0; n < length; n++)
{
if(isValid(array[n]) == 0)
{
n++;
errors++;
}
else
{
printf("%c", array[n]);
}
}
printf("\n");
printf("There were %d errors!\n", errors);
}
int isValid(char ch)
{
if(isalpha(ch) == 0)
{
if(ch != 32)
{
printf("\nCharacter out of bounds, characters are from A to Z or a to z or the space character!!!");
return 1;
}
}
else
{
return 0;
}
}

Unit
05-01-2004, 06:03 AM
Welcome to the forums,

change the scanf statements to these


printf("Enter the key: ");
scanf("%d", keyPtr);
printf("Enter the mode (e = encrypt, d = decrypt): ");
scanf(" %c", &mode);
printf("Enter the string to manipulate (max %d characters): \n", STRINGMAX);
scanf("%s", array);

You dont need /n inside the format string. Also, the extra space infront of %c will prevent you from capturing empty space, tab or Enter key to be read into the char.

The name of the array is an address, you dont have to take address of the array name.

You are right that your IsValid function is causing the problems. try to be consistant with what you are doing with the return value. At some places you are treating as if 0 return value is an error, and at other places you are treating as if its valid!

boris
05-01-2004, 06:36 AM
thanks. i always got the array name being a pointer thing mixed up. pity visual studio didn't pick that mistake up, cause it would have helped if it told me something was wrong instead of skipping it statement.

but what should i do about the isValid error? should i manually state the ASCII codes that are "valid"?

Unit
05-01-2004, 07:24 AM
No, you just need to figure out all the places where you are using it in the wrong way.

Think about this

When you ask the question IsAlpha? do you expect a yes(1) or a no(0)

write your function according to that logic and use it appropriately.

shmoove
05-01-2004, 08:06 AM
By the way, Visual Studio didn't skip any statements. It performs the scanf(), but since you gave it the wrong value (the address containing the pointer instead of the address it points to) it just saved the string in the wrong place. In fact, if array would've been dynamically allocated you would have a little memory leak on your hands.
It's good to keep in mind that computer languages don't skip statements or do anything unpredictable, they do only what you tell them.

shmoove

boris
05-01-2004, 10:19 AM
what i meant was that it didnt wait for me to enter anything in.

isalpha returns 0 if its not alphabetic, so in the case that it does return 0, it checks to see if the character is the space character. if that fails it then tells the user that its out of bounds. but i still dont understand the error it gave me.
The error was:
Debug Assertion Failed!
Program: c:\...\encryption.exe
file: isctype.c
line: 56

Unit
05-02-2004, 03:51 AM
does it still do that after you corrected the input of the string?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum