#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Unhappy I'm stucked here.Please Help me.


    Dear friends,
    I was writing code of simple Mono alphabetic substitution cipher and i got this error. I observed this kind of error previously and it was solved by "getch()" . But unfortunately i am unable to solve this with it. Can you guide me why i am not able to run it correctly at "Runtime" ?

    Let me tell you about the error: When user enters the key value ,the "scanf" is skipped for every even times in while loop.Means User is able to enter key value for first time then second is skipped and third is printed.I don't know the reason behind this.Kindly some one enlighten me. :confused: :confused:

    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char * plainText;
        char key[26];
        char *pointerOfKey=&key;
        char inputCharacter;
        char alphabet[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
        char *pointerOfAlphabet=&alphabet;
    
        printf("NOTE:The alphabet in key shuld be unique for all the Alphabets.\n");
    
        while(*pointerOfAlphabet != NULL)
        {
            printf("Enter Value for:\"%c\"",*pointerOfAlphabet);
            scanf("%c",&inputCharacter);
    
            if(strchr(key,inputCharacter) == NULL)
            {
                *pointerOfKey=inputCharacter;
                pointerOfKey++;
                *pointerOfAlphabet++;
            }
            else
            {
                printf("The input character is alredy inthe key.Inut other.");
            }
        }
    
    
    
    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    scanf("%c",&inputCharacter);
    You are telling scanf to read in the Enter key's code instead of skipping it. How else would you expect your program to behave if you tell it to do such things?

    Now, if instead you were to do this,
    scanf(" %c",&inputCharacter);
    , then you'd be telling scanf to expect white space to precede the character you're interested in and that it should skip that white space. Please note the space in front of the percent sign.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Talking


    Originally Posted by dwise1_aol
    scanf("%c",&inputCharacter);
    You are telling scanf to read in the Enter key's code instead of skipping it. How else would you expect your program to behave if you tell it to do such things?

    Now, if instead you were to do this,
    scanf(" %c",&inputCharacter);
    , then you'd be telling scanf to expect white space to precede the character you're interested in and that it should skip that white space. Please note the space in front of the percent sign.
    Thank you. I got your point.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Closing this thread with this comment.

IMN logo majestic logo threadwatch logo seochat tools logo