![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
I have this code which supposed to pass the user input to other functions and calculate how many $50, $20, $10 bills are needed. User will be prompt to enter a new values when:..
1.) They enter a negative numbers 2.) Enter a number not multiples of 10 However, any numbers beside multiples of 10 loops endlessly and I am not sure what went wrong... #include <stdio.h>
#include "HeaderFile.h"
int main(int argc, char **argv)
{
int num,dummy,five,two,one; /* declare variables*/
int err;
char buf[256];
if (argc==1) /* check the number of arguments -> if no argument*/
{
printf("Please enter the amount:");
scanf("%[^\n]",buf); /* read a line (untill newline is entered)*/
if (sscanf(buf,"%d%d",&num,&dummy)>1) /* scan the string for one or two numbers*/
printf("Warning: only the first number will be used !\n"); /* if more then one number was read -> warning*/
}
else
if (argc>2) /* if two arguments or more are given*/
{
printf("Warning: only the first argument will be used !\n");
sscanf(argv[1],"%d",&num); /* scan the first argument */
}
err = 0;
do
{
if (err)
{
printf("Please enter the amount:");
fflush(stdin);
scanf("%[^\n]",buf); /* read a line (untill newline is entered)*/
if (sscanf(buf,"%d%d",&num,&dummy)>1) /* scan the string for one or two numbers*/
printf("Warning: only the first number will be used !\n"); /* if more then one number was read -> warning*/
}
err = 0;
if(num<0) /* error checking for number <0*/
{
printf("Error: negative number !\n");
err = 1;
}
if(num%10) /* error checking for division by 10*/
{
printf("Error: not a 10 multiple !\n");
err = 2;
}
}
while (err);
five = fifties(&num); /* call the calculating functions*/
two = twenies(&num);
one = tens(&num);
print_results(five,two,one); /* and now print the result*/
return 0;
} |
|
|
|
|
|
#2 | |
|
Programming Guru
![]() |
Quote:
1) they enter a negative number :- shows an error, asks for new user input 2) they enter a non-multiple of 10 :- shows an error message asks for user input. THerefore the program does what is required. EDIT: Unless there is a problem with the functions that we cannot see.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
Weird, I run this in linux with gcc but it just loop through.... perhaps is the difference in compiler and system?..
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
What loops through? Until you enter a value divisible by 10 it will loop.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"fflush (stdin)" is not defined to work, per the standard. Some compilers will implement it, some not. Use "rewind (stdin)." If that does't work, your compiler is standards non-compliant and not just compliant plus tossing in some laniappe.
Your sscanf tends to pick up conditions which you have specified as inappropriate, but a test there doesn't protect you entirely. Always test the return of scanf, also, to make sure input didn't get broken by improper operator input. Once broken, it remains broken until cleared. That means it zoops right on by the call without doing a dam' thang. Clear it with clearerr (stdin), if my memory serves me correctly.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
Hi Berto,
Sorry about the misunderstanding. I mean is that it produce this kind of result when I enter a numbers... Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! Please enter the amount:Error: negative number ! |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jul 2005
Posts: 15
Rep Power: 0
![]() |
Hi DaWei,
Does that means that my linux compiler version is not up to date?... |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
Either as DaWei suggested clear the uinput buffer. I am using Dev-c++ which can do c as well using gcc compiler and it works. The thing i could see going wrong is you are not resetting the num variable, possibly reseting that each time to 0 might help but i doubt it, clear the stdin, i think you can do it using
fflush(); http://man.he.net/man3/fflush fflush(stdin); - not to sure though, above is the man page for it. EDIT: i worked it out ![]()
do
{
if (err)
{
printf("Please enter the amount:");
fflush(stdin);
scanf("%[^\n]",buf); /* read a line (untill newline is entered)*/
if (sscanf(buf,"%d%d",&num,&dummy)>1) /* scan the string for one or two numbers*/
printf("Warning: only the first number will be used !\n"); /* if more then one number was read -> warning*/
}
err = 0;
if(num<0) /* error checking for number <0*/
{
printf("Error: negative number !\n");
err = 1;
}
if(num%10) /* error checking for division by 10*/
{
printf("Error: not a 10 multiple !\n");
err = 2;
}
fflush(stdin); //NEWLINE
}
while (err);
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
since err is an integer, try to explicitly state the conditions for example if (err != 0) and while (err != 0), it helps make your code more readable and easier to debug.
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please note in my previous post that I said that "fflush (stdin)" will work with some compilers. It is not standard, so persisting in its use will open you up to code that won't port if you are actually coding widely and professionally. If you are a hobbist dinking around, you may get away with anything your particular compiler and its runtime library allows.
"Rewind (stdin)" is defined to work. If it doesn't, you are in trouble with your compiler. Non-compliance is more detrimental than compliance with extras. "Rewind (stdin)" is equivalent to an fseek to the beginning of the stream with the added benefit that it also clears stream errors. Refer to any decent C/C++ reference or to the standards document governing your work. Note also that in many systems there are input buffers other than the stream buffer. Keyboard and other OS buffers are an example. Consequently, there is no guaratee that you can clear ALL pending input, if it hasn't yet arrived at the C/C++ controllable streams. In those cases, rawer, non-portable code becomes a necessity if the action is truly warranted in all possible cases. This is distinctly a bite-you-in-the-butt issue if you're doing serious work.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|