![]() |
| | #1 |
| Registered User Join Date: Nov 2007 Location: Portsmouth, England
Posts: 7
| Quick IF statement question (beginner) 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. */
}
}
So, any suggestions (oh and please keep it simple for me to understand!)? Cheers. |
| jim.rattlehead is offline | |
| | #2 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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 12:35 PM. |
| Elysia is offline | |
| | #3 | |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,122
| Quote:
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. | |
| cpjust is offline | |
| | #4 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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*. |
| Elysia is offline | |
| | #5 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| 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, etc. New project: nort |
| dwks is offline | |
| | #6 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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++. |
| Elysia is offline | |
| | #7 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,122
| Oops, I thought it was a char instead of an array of 1 char. |
| cpjust is offline | |
| | #8 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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... |
| Elysia is offline | |
| | #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. */
}
}
How would I go about flushing any input streams of there contents at the top of the function to make sure it is empty? |
| jim.rattlehead is offline | |
| | #10 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| You have two ways: Use fgets and an array: Code: char exitquestion[1000]; fgets(exitquestion, 1000, stdin); Code: char exitquestion;
scanf("%c", &exitquestion);
scanf shouldn't be used to read strings. For that, use fgets, since it's safer. |
| Elysia is offline | |
| | #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. */
}
}
|
| jim.rattlehead is offline | |
| | #12 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Yes, char[1] == char[] == char* char == char == int |
| Elysia is offline | |
| | #13 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| toupper() or tolower() from <ctype.h> will also simplify expressions like Code: if (exitquestion == 'Y' || exitquestion == 'y') Code: if (tolower(exitquestion) == 'y') Code: if (toupper(exitquestion) == 'Y') Code: char exitquestion[1000]; fgets(exitquestion, 1000, stdin); Code: char exitquestion[1000]; fgets(exitquestion, sizeof(exitquestion), stdin); Code: fgets(exitquestion, sizeof exitquestion, stdin); exitfunction() should return void! Do away with the ancient, deprecated implicit int rule! Code: void exitfunction()
{
/* ... */
}
[edit] Quote:
__________________ 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, etc. New project: nort | |
| dwks is offline | |
| | #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 |
| jim.rattlehead is offline | |
| | #15 |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Quick, newbie question: Multiple line string | crummy | C# Programming | 2 | 03-10-2005 06:58 AM |
| Quick Question on File Names and Directories | Kyoto Oshiro | C++ Programming | 4 | 03-29-2002 02:54 AM |
| * quick question * | SavesTheDay | C Programming | 3 | 03-27-2002 06:58 PM |
| just a reaaaaaaly quick question plz help | Unregistered | C++ Programming | 1 | 03-21-2002 11:39 AM |
| Quick question: exit(); | ethic | C Programming | 6 | 08-15-2001 05:46 PM |