Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2004
    Location
    Melbourne, Australia
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Skipping problem in C

    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.
    Code:
    // 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;
    	}
    }

  • #2
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welcome to the forums,

    change the scanf statements to these
    Code:
    	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!
    Last edited by Unit; 05-01-2004 at 07:05 AM.
    Nobody is Perfect. I am Nobody.

  • #3
    New Coder
    Join Date
    May 2004
    Location
    Melbourne, Australia
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"?

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Nobody is Perfect. I am Nobody.

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    May 2004
    Location
    Melbourne, Australia
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by boris; 05-01-2004 at 11:26 AM.

  • #7
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    does it still do that after you corrected the input of the string?
    Nobody is Perfect. I am Nobody.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •