C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-28-2007, 12:20 PM   #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.
jim.rattlehead is offline   Reply With Quote
Old 11-28-2007, 12:31 PM   #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   Reply With Quote
Old 11-28-2007, 02:18 PM   #3
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,122
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.
cpjust is offline   Reply With Quote
Old 11-28-2007, 02:22 PM   #4
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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*.
Elysia is offline   Reply With Quote
Old 11-28-2007, 02:24 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 11-28-2007, 02:33 PM   #6
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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++.
Elysia is offline   Reply With Quote
Old 11-28-2007, 02:38 PM   #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   Reply With Quote
Old 11-28-2007, 02:41 PM   #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   Reply With Quote
Old 11-28-2007, 03:14 PM   #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?
jim.rattlehead is offline   Reply With Quote
Old 11-28-2007, 03:17 PM   #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);
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.
Elysia is offline   Reply With Quote
Old 11-28-2007, 03:24 PM   #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).
jim.rattlehead is offline   Reply With Quote
Old 11-28-2007, 03:26 PM   #12
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Yes, char[1] == char[] == char*
char == char == int
Elysia is offline   Reply With Quote
Old 11-28-2007, 03:27 PM   #13
Frequently Quite Prolix
 
dwks's Avatar
 
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')
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]
Quote:
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 11-28-2007, 03:29 PM   #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   Reply With Quote
Old 11-28-2007, 03:31 PM   #15
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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