Thread: error message

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Unhappy error message

    Hiya, this is probably really simple, but i have this code:

    Code:
    int main () {
    
    char get;
    
    /* Ask if users wants to encrypt or decrpyt */
    printf("Do you want to encrypt or decrypt data? Type E for encrypt, D for Decrypt.:\n");
    scanf ("%c", get);
     if (get == "E")
                  encrypt();
     else
                  decrypt2();
    
     return 0;
     }
    but when I compile it, it says nonportable pointer conversion in function main, at the line: if (get == "E")
    and it crashes if I run it anyway.

    I' ve tried changing this, but still doesn't help. I'm not sure what wrong? please help.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    There are two problems I can see. The first is in your call to scanf. In this you need to include an & in front of the get, because you actually need to send the memory address of the variable not the value (this will probably not mean anything to you, but take my word for it anyway). So it should look like this:
    Code:
    scanf ("%c", &get);
    NOTE: you have to do this with any variable type with scanf, except for a string, also known as a character array.

    Your second problem is as your compiler said in your if statement. When using characters by themselves and not in strings you need to include them inbetween a pair of single quotes, not double. So it would be:
    Code:
     if (get == 'E')
    Hope that helps
    Crazed

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Oooh many thanks
    I am silly becasue I tried putting the & in front, and also the single quotes, but didn't change them both at the same time.
    But thanks for pointing that out to me because I now understand why.
    Cheers

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Before you start to run with something you'll eventually discard, peek at the FAQ.

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed