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 20-May-2006, 08:33
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Need help with Looping


I have setup a vending machine program but I need to use looping to keep asking the user for money and accepting until $1.00 has been entered. For example, if the user enters .25 it will ask them to enter in more money. If they have entered in .25 and they enter in another .25 it should show that their new total is .50. I wanted to make the entire main function loop so that once the transaction is completed, the program will ask if you want to purchase a drink (Y/N) and once Y(yes) is chosen the program would loop again and start over with 0.00 and will ask to insert money again. If N(no) should be selected, the loop should exit and an output statement of what has been purchased should show up.
CPP / C++ / C Code:
#include <stdio.h>
main()
{
float fAnySelection = 1.00; 
int iSelection = 0; 
float f1Payment = 0.00; 
float fChangeDue = 0.00; 
	
printf("\nWelcome to Harry's Retro Soda Pop Vending Machine\n"); 
printf("\nPlease Enter $1.00 to view Various Soda Choices: "); 
scanf("%f%", &fAnySelection); 
	
if (fAnySelection < 1.00){ //$1 inserted is greater than any selection. 
 printf("\nInserting more money guarantees you a drink next time! "); //User has to insert more money to get a drink from vending machine
 printf("Please Enter at the least $1.00: ");
 scanf("%f%", &fAnySelection);}
	
if (fAnySelection  >= 1.00){ //Any selection is greater than or equal to $1
			
 printf("\nEnter 1. for Coca-Cola\n"); //Different types of soda's that the user gets a choice from. Some of these are still around but they have been since the 1970's or even earlier. 
 printf("\nEnter 2. for Pepsi-Cola\n");
 printf("\nEnter 3. for RC Cola\n");
 printf("\nEnter 4. for Diet Rite Cola\n");
 printf("\nEnter 5. for Tab\n");
 printf("\nEnter 6. for Big Red Soda\n");
 printf("\nEnter 7. for Brookdale Orange Soda\n");
 printf("\nEnter 8. for Fresca Soda\n");
 printf("\nEnter 9. for Nehi Grape Soda\n");
 printf("\nEnter 10. for Welch's Grape Soda\n");
 printf("\nPlease Enter Your Selection:");
 scanf("%d%", &iSelection);}
 	
if (iSelection == 1) { 
 printf("\nThanks for your purchase,you made an excellent choice, enjoy and don't forget your drink!\n"); 
			}
	
if (iSelection == 2) { 
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
	
if (iSelection == 3) { 
 printf("\nThanks for your purchase and enjoy. Ohh! and don't forget your drink!\n");
			}
			
if (iSelection == 4) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n"); //Let's user know that the choice they have made was a good idea. 
			}
			
if (iSelection == 5) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
			
if (iSelection == 6) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
			
if (iSelection == 7) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
			
if (iSelection == 8) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
			
if (iSelection == 9) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
			
if (iSelection == 10) {
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
if (iSelection < 11) { //User making a choice greater than number indicated is invalid
 printf("\nThe Choice you have selected is currently invalid. ");
 printf("Please Try Again: ");
 scanf("%d%", &iSelection); }
	
fChangeDue=fAnySelection-1.00;{ 
if (fChangeDue>0.00);
 printf("\nAmount of change owed to user $%.2f", fChangeDue);} //Shows change owed to customer, if any that is owed. 
			
getchar();
			
}
  #2  
Old 20-May-2006, 10:40
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 872
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help with Looping


Harry, was not the other post with a loop for your nearly-same code helpful?

see here: http://www.gidforums.com/showpost.ph...8&postcount=11
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 20-May-2006, 14:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,230
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

Re: Need help with Looping


Quote:
Originally Posted by Harryt123
I have setup a vending machine program but I need to use looping to keep asking the user for money and accepting until $1.00 has been entered. For example, if the user enters .25 it will ask them to enter in more money. If they have entered in .25 and they enter in another .25 it should show that their new total is .50.
Look into the while statement for this loop.

Quote:
Originally Posted by Harryt123
I wanted to make the entire main function loop so that once the transaction is completed, the program will ask if you want to purchase a drink (Y/N) and once Y(yes) is chosen the program would loop again and start over with 0.00 and will ask to insert money again. If N(no) should be selected, the loop should exit and an output statement of what has been purchased should show up.
Ask the question at the end of the program and look at the do...while statement for this one. Decide where in the program you want to return to and that's where the start of the loop should go.

Now some comments on the program itself:
CPP / C++ / C Code:
#include <stdio.h>
main()
main() is always and int... Some compilers will complain if you don't specify. Learn to use the correct form. And the end of the program you need a return statement that returns an int value

CPP / C++ / C Code:
{
float fAnySelection = 1.00; 
int iSelection = 0; 
float f1Payment = 0.00; 
float fChangeDue = 0.00; 
Open brace -- indent!!!

CPP / C++ / C Code:
printf("\nWelcome to Harry's Retro Soda Pop Vending Machine\n"); 
printf("\nPlease Enter $1.00 to view Various Soda Choices: "); 
scanf("%f%", &fAnySelection); 
What's %f%? What's with the second %? %f is enough.

CPP / C++ / C Code:
if (fAnySelection < 1.00){ //$1 inserted is greater than any selection. 
 printf("\nInserting more money guarantees you a drink next time! "); //User has to insert more money to get a drink from vending machine
 printf("Please Enter at the least $1.00: ");
 scanf("%f%", &fAnySelection);}
	
if (fAnySelection  >= 1.00){ //Any selection is greater than or equal to $1
If they still didn't enter enough money, you're going to give them the drink anyway. All you are doing with the second if is avoiding the selection. You still process the selection.

CPP / C++ / C Code:
if (iSelection == 1) { 
 printf("\nThanks for your purchase,you made an excellent choice, enjoy and don't forget your drink!\n"); 
			}
	
if (iSelection == 2) { 
 printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
			}
...
Booooringgggggg! Why say the same thing (or nearly the same thing) for each and every selection? Once is enough as long as they choose a valid drink. If you need a 'special message' for a drink, display the main message at the top of this list, then for the few special messages output it with this
if list.

CPP / C++ / C Code:
if (iSelection < 11) { //User making a choice greater than number indicated is invalid
 printf("\nThe Choice you have selected is currently invalid. ");
 printf("Please Try Again: ");
 scanf("%d%", &iSelection); }
Is this if correct? And what if they enter another invalid selection?

CPP / C++ / C Code:
fChangeDue=fAnySelection-1.00;{ 
if (fChangeDue>0.00);
 printf("\nAmount of change owed to user $%.2f", fChangeDue);} //Shows change owed to customer, if any that is owed. 
Look closer at this section....

Be more consistant in your indenting and brace placement. It will help you find errors quicker. See this for some ideas
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #4  
Old 21-May-2006, 12:16
Harryt123 Harryt123 is offline
New Member
 
Join Date: May 2006
Posts: 14
Harryt123 is on a distinguished road

Re: Need help with Looping


How would I put into code where it asks the user for money(for example, if they insert .25. It will ask them for additional .75) because drinks cost $1.00. I am having the hardest time trying to figure that part of it out. Any help would be greatly appreciated. Thank you for the suggestions and I did make some of those changes and my program is working better than I had it before. Now all I need help in is what was mentioned above.
  #5  
Old 21-May-2006, 14:45
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,230
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

Re: Need help with Looping


Code:
Set MoneyEntered to 0.0 While MoneyEntered is not 1.00 Ask for money, display (1.00 - MoneyEntered) Input Money add Money to MoneyEntered end-of-while
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 

Recent GIDBlogMaster?s Degree by crystalattice

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
Looping fj8283888 CPP / C++ Forum 8 11-Apr-2006 21:12
Sample looping (do/while) Sabin044 CPP / C++ Forum 8 04-Feb-2006 11:39
Problem with looping. rina C Programming Language 2 04-Oct-2005 06:37
ask looping likeit C Programming Language 4 26-Jul-2005 19:49
for looping prob Rosmayati C Programming Language 1 30-Jun-2004 11:39

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

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


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