![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2008
Location: Florida
Posts: 8
Rep Power: 0
![]() |
looping fun
Hi all! I'm writing this little program that functions as a very rudimentary calculator. I'll be honest, it is part of an exercise from the tutorial I'm using but I am just stumped. What it's supposed to do is ask for two floats as well as a char that will tell it whether it will add, subtract, multiply and divide. It's also supposed to loop and keep asking for numbers and a char until its passed the q char, in which case it's supposed to quit. Here's what it's ACUALLY doing:
1. It will ask for the first and second number. 2. It will not ask for the char and instead asks for the first number again. 3. When is asks for the first number again, it also runs the code in the default case. 4. If I give the char when it asks for the first number (for the second time) it will run the proper case and calculation. I know I'm overlooking SOMETHING here and know me, it's something pretty obvious but I just don't know what it is. Here's the code: c Syntax (Toggle Plain Text)
I can't for the life of me understand why its skipping the scanf() at line 24 nor why it's acting like it is. I'm not asking for an answer as we all know that won't help me learn but maybe just a nudge in the right direction. |
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,009
Rep Power: 5
![]() |
Re: looping fun
I'm not gonna solve this one for you, but I'll give you a couple of suggestions.
First, whenever receiving input from the user- or any other source not under your absolute control (disk file, network connection, etc), you need to verify that the input given is indeed the type of input you expected. With scanf(), this means checking the return value. It will be EOF if there is an error (such as hitting end-of-file, trying to read from a closed stream, or having some hardware error). Otherwise, it will be the number of items successfully read in (scanf() will stop as soon as it hits bad input, so if the first, second, and fourth items were entered correctly, but the third wasn't, the return value will be 2, not 3). If you're asking for a single number, then the return value will be 1 if everything went as you hoped. If it's zero, it means the user did something stupid, such as entering a string. If it's EOF, you have an error as described above. You might find it easier to wrap the scanf() calls inside a function that does this kind of error-checking logic, and will re-prompt the user if they entter bad data, or alert them if something is wrong beyond your control. Then you can call getNumber() or something rather than scanf(), and know that your bases are still covered.Second, remember that I/O in C is generally line buffered. In other words, when you read with scanf(), it will not actually read anything in until the user hits enter. Thus, the user must type a character followed by enter, rather than just the character itself. There are various ways around this, but many (most?) are not entirely portable. You might check and see if your compiler has the conio.h header file; if it does, there are often functions in there to read single characters, as well as special characters such as function keys.On another note, kudos to you for using code tags, and posting an intro before a question. So few new users do either these days, much less both, and it's refreshing to see. ![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Aug 2008
Location: Florida
Posts: 8
Rep Power: 0
![]() |
Re: looping fun
After battling this exercise all day, I have a new found respect for programmers. I have always read that computers are inherently dumb and most be told every stinking thing to do. I never realized how true that statement is until I was the one behind the drivers seat. I've had to really change my way of thinking to get it done. I shudder to think what assembly programmers have to go through... Well enough of my little rant. Here's my progress so far.
I stripped down the code to just it asking for the two floats and a char. I also added another printf() at the end so that I could see what exactly the scanf() asking for char was doing. Sure enough, it would be output NULL. I changed the char to a char array and had scanf() look for a string instead of a character and IT WORKED! .....but I have no idea why. When scanf() is asking for a %c and I input a single character, it is my understanding that that is all I am giving it. Or is it? I know in strings, I have to account for the string terminator (NULL or \0 I believe). Is there also a terminator in a char variable as well? Is scanf() just funny that way? Or is it something else I'm missing? Well now that I know the input works, I had to see if the calculation part was working. I did some trial and error (mainly error) with the switch/cases as well but that didn't take too long to figure out. It doesn't loop to ask for a new set of numbers but it does the proper calculation based on what character you input. Here's the code as it stands now: c Syntax (Toggle Plain Text)
I know you said it would be better to wrap scanf() in another function and have it check for the proper type but I think that's a bit beyond me at the moment. The guide I'm following so far has taught me basic I/O(printf() and scanf()), arrays, do/while, switch/case, and if/else. Functions are in the next chapter. What sucks is that there are 3 exercises and this is only the first... I feel like I'm just asking for more punishment. (Thank you sir! May I have another!?) I'm still fighting with the loop but I will continue that battle tomorrow. My brain is a bit fried at the moment. |
|
|
|
|
|
#4 | |||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,009
Rep Power: 5
![]() |
Re: looping fun
Quote:
scanf() or its brethren (fscanf(), etc) to read something like a number, it will need to convert it. The value 40 is not the same as the value "40". The first is a single value, of int type by default. The latter is a const pointer to a const sequence of characters, being '4', '0', and a char with a binary value zero ('\0' or (char)0). If you're familiar with ASCII or other text encoding methods (Unicode, etc), you will remember that a single digit's ASCII (or whatever) value is not necessarily the same as the value that digit represents. Here's a table.Basically what I'm getting at is that because the input is already in string format, scanf() doesn't have to do any conversions, and basically cannot fail. More precisely, it should only return EOF on an error as described in my initial post, and should never return zero (actually, it might, if the string address you pass in is NULL, but that's another discussion). However, you do have to watch out for other things when inputting strings, though; foremost among these would be the dreaded 'buffer overflow' error. This happens when you allocate space for x characters (plus the terminating zero), and the user inputs more than x characters. It's also why languages such as C# and C++ that have dedicated string classes make this sort of problem a lot easier to avoid.Quote:
This means that scanf() will fill in the char variable whose address you pass with the first non-whitespace character the user enters. If the user doesn't enter anything, it will not fill anythign in (ie, scanf() will successfully convert fewer than the requested number of items).Thus, if the single character you want to read is potentially a whitespace character, scanf() really isn't the tool for the job. You'd be better off using fgetc() for this:char inputChar = fgetc(stdin); getchar() macro if you want to read from standard input; this is just an alias for fgetc(stdin). I'd recommend the former though, as it will make your code easier to modify if you later choose to read from a stream besides standard input (such as if you want your program to process a text file of commands for 'batch mode' operation).Quote:
![]() Anyways, I think you should read ahead. If you don't get something in the current chapter, later chapters might shed some light. Often, with programming, you have knowledge with circular dependencies; to fully understand concept A, you may need some knowledge of concept B, and vice-versa. In particular, I'm surprised you can make any sense of scanf() without at least a basic knowledge of pointers, which is probably not something you've covered yet.Anyways, apologies if this post was less than coherent. I just got home a little while ago, and my brain isn't working too well at the moment. ![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|||
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2008
Location: Florida
Posts: 8
Rep Power: 0
![]() |
Re: looping fun
So basically, when I have scanf() ask for a character like I did, scanf() still considers the input a string even though I'm inputting a single character? Well that makes sense why that works now.
Thanks for your help pharaoh! I'll most likely be back for more when I get home to tackle the looping part of it. ![]() |
|
|
|
|
|
#6 | ||
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,009
Rep Power: 5
![]() |
Re: looping fun
Quote:
scanf() to read. Thus, if you're looking for a string, no conversion is necessary (as long as scanf() is able to read something, it will work). Remember that the string can be zero characters plus the terminating zero.If you want a single character, like I said, use a different function; this will allow you to check for whitespace characters as well. Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
||
|
|
|
|
|
#7 |
|
Newbie
Join Date: Aug 2008
Location: Florida
Posts: 8
Rep Power: 0
![]() |
Re: looping fun
VICTORY!
It asks for the two numbers and a which calculation you want done. It also keeps looping until you give it the q command in which case it quits. I know you're all probably thinking "Hoohum, I could do that in my sleep," but I feel like I'm finally getting somewhere. I can't wait till I get to the pointers section that I hear everyone had such a hard time with! C Syntax (Toggle Plain Text)
Feel free to criticize away! Please let me know if I can do anything better. Better that I hear about it now than learn about it later on the hard way. |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 175
Rep Power: 1
![]() |
Re: looping fun
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to. |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 175
Rep Power: 1
![]() |
Re: looping fun
This thread reminds me of when I was busy with the same type of program long time ago, when I started learning C.
see here I know it sucks, but it might show you what not to do, hehe ![]()
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Aug 2008
Location: Florida
Posts: 8
Rep Power: 0
![]() |
Re: looping fun
I looked through some of that code and came across this little gem:
return (main()); I was like "wha...?" but then Grumpy's post cleared that all up. Real nice. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| error trappring and looping | mrynit | Java | 5 | Jan 22nd, 2007 7:58 AM |
| Looping effect, when entering string in an int variable | lamefif | C++ | 7 | Jan 5th, 2006 11:29 AM |
| Endless looping >_< | jch02140 | C | 13 | Aug 4th, 2005 6:43 AM |
| looping in awk | gwilliam | Sed and Awk | 3 | Jun 20th, 2005 9:12 AM |