Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 1st, 2005, 10:21 AM   #1
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Question Endless looping >_<

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;
}
jch02140 is offline   Reply With Quote
Old Aug 1st, 2005, 10:43 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
Quote:
Originally Posted by jch02140
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...
Isnt it ment to do that or do you want it to exit when the user enters a number not a multiple of 10, because at the minute it works when you enter a multiple of 10 that is positive it works as far as i can see, this program is doing what you have specced.

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
Berto is offline   Reply With Quote
Old Aug 1st, 2005, 11:13 AM   #3
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Weird, I run this in linux with gcc but it just loop through.... perhaps is the difference in compiler and system?..
jch02140 is offline   Reply With Quote
Old Aug 1st, 2005, 11:21 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Old Aug 1st, 2005, 11:22 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"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
DaWei is offline   Reply With Quote
Old Aug 1st, 2005, 11:44 AM   #6
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
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 !
jch02140 is offline   Reply With Quote
Old Aug 1st, 2005, 11:44 AM   #7
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Hi DaWei,

Does that means that my linux compiler version is not up to date?...
jch02140 is offline   Reply With Quote
Old Aug 1st, 2005, 11:57 AM   #8
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
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
Berto is offline   Reply With Quote
Old Aug 1st, 2005, 12:03 PM   #9
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Old Aug 1st, 2005, 12:11 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC