Quick IF statement question (beginner)

This is a discussion on Quick IF statement question (beginner) within the C Programming forums, part of the General Programming Boards category; Hi there, I am new to C and I having trouble with this if statement which is called by the ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7

    Quick IF statement question (beginner)

    Hi there, I am new to C and I having trouble with this if statement which is called by the main function when the user requests to quit:

    Code:
    exitfunction()
    {
        char exitquestion[1]; /* Local variable for storing question response about quitting. */
        printf("\n\nAre You Sure You Wish To Quit? [Y]es Or [N]o: ");
        scanf("%c", &exitquestion);
        if (exitquestion[0] == 'Y' || exitquestion[0] == 'y') { exit(0); } /* Returns exit code 0 to OS */
        else { 
             if (exitquestion[0] == 'N' || exitquestion[0] == 'n') { main(); } /* Back to start. */
             else {printf("\n\nOnly [Y]es Or [N]o are acceptable inputs!"); exitfunction(); } /* If user enters neither 'Y' or 'N' then go back and ask again. */
             }
    }
    The problem is, when the function is called, it prints the 'Are you sure you wish to quit?' question immediately followed by the error 'Only Y or N are acceptable inputs' message, without prompting for a input. Thus is does what is meant to do in that situation and loops back to the top, this time it asks for the neccessary input. I am completly stumped by this, as surely it should pause for input every time the function is run.

    So, any suggestions (oh and please keep it simple for me to understand!)?

    Cheers.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Exitquestion variable should be at least 2 bytes long, because even the simplest string is char + NULL (so it will Y + NULL here).
    Don't do recursion - use a loop instead.
    scanf is also very dangerous because it doesn't take into mind the buffer size. Recommend use fgets instead.
    I believe you're calling scanf wrong too. Try removing & before exitquestion.
    exitfunction should return something - and if it returns nothing, then return void.
    Last edited by Elysia; 11-28-2007 at 11:35 AM.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Exitquestion variable should be at least 2 bytes long, because even the simplest string is char + NULL (so it will Y + NULL here).
    Don't do recursion - use a loop instead.
    scanf is also very dangerous because it doesn't take into mind the buffer size. Recommend use fgets instead.
    I believe you're calling scanf wrong too. Try removing & before exitquestion.
    exitfunction should return something - and if it returns nothing, then return void.
    Actually scanf() requires the & before exitquestion because you have to pass it by reference so scanf() can change the value.

    My guess is there's probably something left in the input buffer from a previous scanf(), gets() or whatever. But fgets() is probably a better choice.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Quote Originally Posted by cpjust View Post
    Actually scanf() requires the & before exitquestion because you have to pass it by reference so scanf() can change the value.
    Yes, but I'm concerned that arrays are usually passed as references/pointer by default, by just specifying the name, at least in C++. So &name would actually become a char** and not char*.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Elysia, you are correct -- the OP is inadvertently creating a char** with their use of & on a char[] array. (So first you have C on your mind, and now you're thinking of C++? )

    The OP is getting confused. You can either read in a character, in which case you use a char variable and and &. Or you can read in a string, in which case you use a char[] array and no &.

    There's no need to declare an array of one element. You can just use
    Code:
        char exitquestion; /* Local variable for storing question response about quitting. */
        printf("\n\nAre You Sure You Wish To Quit? [Y]es Or [N]o: ");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Quote Originally Posted by dwks View Post
    Elysia, you are correct -- the OP is inadvertently creating a char** with their use of & on a char[] array. (So first you have C on your mind, and now you're thinking of C++? )
    Lol, no, I'm just sticking to my programming style. I'm a C++ programmer at heart, but C is similar to C++ due to C++ originally being an extension of C, so I don't know if the same rules apply, but I do assume so. C-style casts is what I've always done and only recently tried to use C++-style casts. So I was assuming it became a char**, since that's would it would become in C++.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Oops, I thought it was a char instead of an array of 1 char.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Hehehe, we all make mistakes. It splipped my mind that you could just use a char to take input too Which would make sense in this case...

  9. #9
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    Thanks for the help everyone, have tried this:

    Code:
    exitfunction()
    {
        char exitquestion; /* Local variable for storing question response about quitting. */
        printf("\n\nAre You Sure You Wish To Quit? [Y]es Or [N]o: ");
        fgets(exitquestion, 2, stdin);
        if (exitquestion == 'Y' || exitquestion == 'y') { exit(0); } /* Returns exit code 0 to OS */
        else { 
             if (exitquestion == 'N' || exitquestion == 'n') { main(); } /* Back to start. */
             else {printf("\n\nOnly [Y]es Or [N]o are acceptable inputs!"); exitfunction(); } /* If user enters neither 'Y' or 'N' then go back and ask again. */
             }
    }
    But it gives me a 'makes pointer from integer without a cast' error. Is the fgets reading in a string and comparing it to a char?

    How would I go about flushing any input streams of there contents at the top of the function to make sure it is empty?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    You have two ways:
    Use fgets and an array:
    Code:
    char exitquestion[1000];
    fgets(exitquestion, 1000, stdin);
    Or use scanf and a char:
    Code:
    char exitquestion;
    scanf("%c", &exitquestion);
    Both are safe, but maybe the second makes more sense in your case.
    scanf shouldn't be used to read strings. For that, use fgets, since it's safer.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    Wow speedy reply! So an array of char[1] is different to a variable of char? Didn't realise that. I now have this, which is similair to my original code:

    Code:
    exitfunction()
    {
        char exitquestion; /* Local variable for storing question response about quitting. */
        printf("\n\nAre You Sure You Wish To Quit? [Y]es Or [N]o: ");
        scanf("%c", &exitquestion);
        if (exitquestion == 'Y' || exitquestion == 'y') { exit(0); } /* Returns exit code 0 to OS */
        else { 
             if (exitquestion == 'N' || exitquestion == 'n') { main(); } /* Back to start. */
             else {printf("\n\nOnly [Y]es Or [N]o are acceptable inputs!"); exitfunction(); } /* If user enters neither 'Y' or 'N' then go back and ask again. */
             }
    }
    But it still gives me the same problem. Without using the & I get a segmentation fault. (Using GCC).

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Yes, char[1] == char[] == char*
    char == char == int

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    toupper() or tolower() from <ctype.h> will also simplify expressions like
    Code:
    if (exitquestion == 'Y' || exitquestion == 'y')
    to
    Code:
    if (tolower(exitquestion) == 'y')
    or
    Code:
    if (toupper(exitquestion) == 'Y')
    Code:
    char exitquestion[1000];
    fgets(exitquestion, 1000, stdin);
    sizeof() never hurt anyone. (If it's an array and not a pointer.)
    Code:
    char exitquestion[1000];
    fgets(exitquestion, sizeof(exitquestion), stdin);
    or
    Code:
    fgets(exitquestion, sizeof exitquestion, stdin);
    You should be aware that main() recursion (calling main()) is undefined in C++. It's also somewhat suspicious practise in C -- you should be able to code in such a way that you can use iteration (i.e., loops) instead.

    exitfunction() should return void! Do away with the ancient, deprecated implicit int rule!
    Code:
    void exitfunction()
    {
        /* ... */
    }
    Also consider better indentation . . . .

    [edit]
    char == char == int
    Debatable . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Portsmouth, England
    Posts
    7
    Hmm I see. So for a implementation like the above it is always best to just use a char variable and not an array. Although either way it doesn't seem to be changing the behaviour of the function.

    Cheers,

    James

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,414
    Quote Originally Posted by dwks View Post
    [edit]
    Debatable . . . [/edit]
    Perhaps... but the fact is that the compiler does treat char as int, and vice versa can also be true.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick, newbie question: Multiple line string
    By crummy in forum C# Programming
    Replies: 2
    Last Post: 03-10-2005, 05:58 AM
  2. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 01:54 AM
  3. * quick question *
    By SavesTheDay in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 05:58 PM
  4. just a reaaaaaaly quick question plz help
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 10:39 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21