GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Apr-2004, 15:34
McFury McFury is offline
New Member
 
Join Date: Apr 2004
Posts: 3
McFury is on a distinguished road

Need help with a C program (Long)


Hi all,
Im pretty new to C, so please forgive me if my code doesnt make sence in parts
Anyways Im writing the second program of my CS class assigment but my program seems kind of all over the place at the moment.

The question that im answering is:
You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step.

Some of the problems i've been getting are:
1) if i enter a character instead of a number at the start of the program where it asks for the users input, the program just keeps repeating that character infinatly down screen and i dont know how to make so it only accepts an integer input (so that if anything else is entered , the error message is triggered).
2) if i enter a number that is not in an increment of 5 it displays the error message fine, but then also displays the 'amount of coins' part and prints out a bunch of crazy numbers like
2147348480 Fifty cent piece's.
1 Ten cent piece.
and I dont know what to do to stop it doing that
3) is there a way to show the original amount that the user entered, in the printf statement down the bottom of the program when it displays the coins needed?

This is what ive done so far:

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>

/*******************************************************************************
********************************************************************************
			  MAIN PROGRAM STARTS HERE
********************************************************************************
*******************************************************************************/


int main()


{
	
	//Variables 

	int fifty, twenty, ten, five, result;
	char option;

	printf("\nThis program decides how many, and of what type of coins need to be returned\nto the customer.");    //If i enter a character here it goes crazy and spams continuously.
	
	do	
 	{	
	printf("\n\nDo you wish to continue? (Y/N): ");

	//MAKES UPPER OR LOWER CASE CHARACTERS INTO THIER OPPOSITE VALUE.
	
	option = getch();
	option = toupper (option);
	}
	while (!((option=='Y') || (option=='N'))); 
		printf("%c\n",option);
	if (option=='Y')
	{
			
			while (1)
			{
			//GATHERS INPUT FROM USER.

			printf("\nEnter the amount of cents that needs to be returned to the customer: ");
			scanf("%d", &result);

			if ((result<0) || (result>95) || (result%5>0))
				printf("\nYou cannot enter an amount less then 0 and greater than 95.\nNumbers must be in increments of 5\n");
			else
	  
		 	//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
		
			fifty = result / 50;
			result %= 50;
			twenty = result / 20;
			result %= 20;
	      		ten = result / 10;
	      	 	result %= 10;
			five = result / 5;
			result %= 5; 
		
                                      //AMOUNT OF EACH COIN BEING RETURNED.
                                       
                                      //Can I make this show the original amount that the user entered? ie. "To make change for 65 cents, you will need the following coins" ?

			printf("To make change for the amount you entered, you will need the following coins:\n\n", result);
		
			if (fifty > 1)
    				printf("%d Fifty cent piece's.\n", fifty);
			if (fifty == 1)
				printf("%d Fifty cent piece.\n", fifty);
			if (twenty > 1)
       				printf("%d Twenty cent piece's.\n", twenty);
			if (twenty == 1)
       				printf("%d Twenty cent piece.\n", twenty);
			if (ten > 1)
       				printf("%d Ten cent piece's\n", ten);
			if (ten == 1)
       				printf("%d Ten cent piece.\n", ten);
			if (five > 1)
       				printf("%d Five cent piece's\n", five);
			if (five == 1)
				printf("%d Five cent piece.\n", five);
			}
			
	}	
return(0);
}


/*******************************************************************************
********************************************************************************
			  MAIN PROGRAM ENDS HERE
********************************************************************************
*******************************************************************************/

Any help would be great
thanks.
  #2  
Old 29-Apr-2004, 16:39
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello McFury. Welcome to GIDForums. Congratulations on using the c-syntax highlighting on your first post! That is a rarity around these parts

Anyway, one thing that I notices right off is that your program is not modular. I don't know if you want to get this running and then split it into functions, but just keep that in mind.

Overall, I think that you have a great start on this and it is a pretty decent program as well as easy to read. I will try to answer your questions the best I can...

Quote:
1) if i enter a character instead of a number at the start of the program where it asks for the users input, the program just keeps repeating that character infinatly down screen and i dont know how to make so it only accepts an integer input (so that if anything else is entered , the error message is triggered).
This is probably due to the getch function. Are you familiar enough with the fgets function to use it? You could do something like this:
CPP / C++ / C Code:
char option[5];  //Define this as a string (character array)
...

fgets(option,4,stdin);  //This will read a string of up to 4 chars from the console
option[0] = toupper (option[0]);  //instead of option for your char use option[0]
...//etc
This will allow people to enter Yes or No and still be okay. If this is unclear though, you may want to use something else.

Quote:
2) if i enter a number that is not in an increment of 5 it displays the error message fine, but then also displays the 'amount of coins' part and prints out a bunch of crazy numbers like
2147348480 Fifty cent piece's.
1 Ten cent piece.
and I dont know what to do to stop it doing that
This is caused by your else statement not enclosing all of the statements that you wish to skip. This should look more like:

CPP / C++ / C Code:
if ((result<0) || (result>95) || (result%5>0))
				printf("\nYou cannot enter an amount less then 0 and greater than 95.\nNumbers must be in increments of 5\n");
			else{   //Notice the added bracket....
	  
		 	//FINDS HOW MANY OF EACH COIN WILL BE NEEDED.
		
			fifty = result / 50;
			result %= 50;
			
                        .....  

			if (five == 1)
				printf("%d Five cent piece.\n", five);
        } //Closing bracket for the else statement.
Quote:
3) is there a way to show the original amount that the user entered, in the printf statement down the bottom of the program when it displays the coins needed?
Certainly, just make another variable to copy it into before you molest it.
CPP / C++ / C Code:
int fifty, twenty, ten, five, result,original;   //New variable called original
	char option;
 ....
                        original = result;   //I added this line
                        fifty = result / 50;
                        
                        ....			

			result %= 5; 
		
                                      //AMOUNT OF EACH COIN BEING RETURNED.
                                       
 printf("To make change for %d cents, you will need the following coins:\n",original)

Hope this helps. Good luck,
d
  #3  
Old 29-Apr-2004, 17:41
McFury McFury is offline
New Member
 
Join Date: Apr 2004
Posts: 3
McFury is on a distinguished road
Thanks heaps for your reply dsmith, it was very helpful :-)

The only thing that seems to still be causing a problem is when i enter a character(or any key thats not a number) at any time from this statement, onwards it still repeats the character infinately down the screen.

CPP / C++ / C Code:
printf("\nEnter the amount of cents that needs to be returned to the customer:  \n");
scanf("%d", &result);

also, yes I am suppose to present the program and algorithm in modular form but i couldnt remember 'exactly' what was meant by modular

thanks in advance.
  #4  
Old 29-Apr-2004, 20:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,432
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by McFury
Thanks heaps for your reply dsmith, it was very helpful :-)

The only thing that seems to still be causing a problem is when i enter a character(or any key thats not a number) at any time from this statement, onwards it still repeats the character infinately down the screen.

CPP / C++ / C Code:
printf("\nEnter the amount of cents that needs to be returned to the customer:  \n");
scanf("%d", &result);
That's because you are using scanf(). This function will not work properly if you don't give it exactly what you ask of it -- in your case, an integer (%d). Any non-number will cause the problem you see.

Are you required to "error proof" the program?
1) If not, don't bother at this early stage of your programming.
2) If so, you will have to leave scanf() behind and use a combination of fgets()/sscanf() or something similar. This gets difficult.

Quote:
Originally Posted by McFury
also, yes I am suppose to present the program and algorithm in modular form but i couldnt remember 'exactly' what was meant by modular
Segment the program into pieces:
1) Input section
2) Computation section
2a) 50 cent
2b) 25 cent
2c) 10 cent
2d) 5 cent
3) Output section

If you are using functions, place the segments in separate functions. If not, write each section of the code so it can stand alone.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
 
 

Recent GIDBlogCompress Your Web Site by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a C program through Linux shell script nuwandee C Programming Language 3 29-Mar-2004 21:54
Modify a C program ayoub C Programming Language 3 15-Mar-2004 11:34
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 10:22

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:04.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.