Help me..I'm stucked here.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 #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.");
        }
    }




}
why use printf and scanf? Isn't that c and not c++?
better off using cout << and cin >> or getline(cin, string)
other than that I don't know what to tell you.
@giblit

Looks like herold is doing C code as you said. Although the string header is in included but not used - so it's difficult to know what the real intention is.

@herold a couple number of things:

The name of a an array variable is implicitly converted to a pointer - so taking the address of it on line 8 & 11 isn't needed.

Line 10 looks like an error, the correct way to initialise an array specifies each char in single quotes separated by commas.

If you are going to do strings with pointers, you need to put the closing '\0' on the end manually with an assignment. So line 15 won't work unless you do this.

scanf returns a value which shows how successful it was. You should always test this value before using any variable it reads in - especially when reading more than 1, as sometimes only some of them might succeed. Remember you are dealing with a user that types at the console.

Same goes for any function that returns an error checking value. An example might be fprintf - I always use sprintf first - if that works as intended then use fprintf to send the string to the file. Always read the reference material for any function that you use.

Hope all goes well.


Another easy fix for line 10 would to make it a string
like
string alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
then when you want to call each individual character do something like
1
2
3
 alphabet[numberofletterinalphabet-1]
//ex: 0 = a
        25 = z
Last edited on
Topic archived. No new replies allowed.