1,105,644 Community Members

Vending Machine

Member Avatar
phish1429
Newbie Poster
1 post since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Yes i know everyone hates homework help but this was a last place i could go I am writing a program that stimulates a vending machine and i have written almost all of the code its just not working correctly I am stressing out because it is due by tomorrow and was wondering if anyone would be able to help me out in any way possible i have attached the *.cpp file and would be willing to do anything if someone would look over it Im not asking for you to completly due it just help me becase i am stuck and do not know were to turn.
In the program it is to give change back which i am having trouble doing so because it breaks it down into half dollars dollars dimes quarters and nickles its really the only problem i have i have written code that i believed to work but it has not if anyone is out there please help
This is what the outcome should look like http://www.cs.ua.edu/114/Summer2004/Programs/prog2.htm

Attachments azar_thomas.cpp (2.43KB)
Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

Gee, you sure were in a hurry but the lack of full stops really mangles what you say.

This is not really a place to ask for homework answers.But I did look at your code and you use a lot of variables like a,b,c etc for what resonable puprose I cant guess.Also code is a bit messed up in the sense that it too packed where you calculate the vals so I could not understant it too much either.

But here's something :
Lesson1:Code clearly and use spaces and newlines.They will save your life when it comes to debugging.Noodle code is a death trap.(Your code is not too noodlely except in the if else part).

Lesson 2:Use funtions (I dont know if you know funtions)

Now your prob:One Idea would be to convert the entire amount in to the lowest denomintions availble(nickle?) and calulate using that.In the end convert it back to dollars and show what ever remanins as nickles.Dont bother with quaters unless you have to.
Not much but i could not understand you code nor what was you prob,nor do i have a compiler handy right now. :0

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm writing your program though it might be too late! ;)

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ok... it's ready but it doesn't cover the part where the option is invalid ;)

#include <iostream.h>
#include <conio.h>
int dol,cen;
unsigned int cb=0,ch;
void convert(int cb)
{
     dol=0;cen=0;
     while (cb>=100) {
		     cb-=100;
		     dol++;
		    }
     cen=cb;
}

void read()
{
		convert(cb);
		cout <<"The current balance is\t" <<dol << " dollars and "<< cen <<" cents "<<endl;
		cout <<"Please choose from one of these options"<<endl;
		cout <<"\n";

		cout <<"5 -	Deposit a nickle\t";
		cout <<"45 -	Buy some gum for 45 cents"<<endl;

		cout <<"10 -	Deposit a dime\t"<<"\t";
		cout <<"55 -	Buy crackers for 55 cents"<<endl;

		cout <<"25 -	Deposit a quarter\t";
		cout <<"60 -	Buy a soft drink for 60 cents"<<endl;

		cout <<"50 -	Deposit a half dollar\t";
		cout <<"70 -	Buy a candy bar for 70 cents"<<endl;

		cout <<"100 -	Deposit a dollar bill\t";
		cout <<"85 -	Buy some chips for 85 cents"<<endl;

		cout <<"0 -	Request coin return and quit\t";

		cout <<"\n";

		cout << "\n" <<"Enter a number to choose an option:\t";
		cin >> ch;
}
void main()
{
       clrscr();
       cout <<"This program simulates a vending machine"<<endl;
       read();
       while (ch!=0)
		{
		 cout<<"\n\n";
		 if (ch==5) { cb+=5;ch=-1;}
		 if (ch==10){ cb+=10;ch=-1;}
		 if (ch==25) {cb+=25;ch=-1;}
		 if (ch==50) {cb+=50;ch=-1;}
		 if (ch==100) {cb+=100;ch=-1;}

		 if (ch==0)
			{
			 cout<<"Quiting\n";
			 while (cb%100==0&&cb>0) {cout<<"Returning a dollar\n";cb-=100;}
			 while (cb%50==0&&cb>0) {cout<<"Returning a half dollar\n";cb-=50;}
			 while (cb%25==0&&cb>0) {cout<<"Returning a quarter\n";cb-=25;}
			 while (cb%10==0&&cb>0) {cout<<"Returning a dinme\n";cb-=10;}
			 while (cb%5==0&&cb>0) {cout<<"Returning a nickel\n";cb-=5;}
			 ch=-1;
			}

		 if (ch==45)
			{
			if (cb>45) {
				    cout<<"Here is your gum\n";
				    cb-=45;
				   }
				else cout<<"Error:Deposit more money\n";
			ch=-1;}
		 if (ch==55)
		 {
			if (cb>55) {
				    cout<<"Here are your crackers\n";
				    cb-=55;
				   }
				else cout<<"Error:Deposit more money\n";
			ch=-1;}
		 if (ch==60)
			{
			if (cb>60) {
				    cout<<"Here is your soft drink\n";
				    cb-=60;
				   }
				else cout<<"Error:Deposit more money\n";
			ch=-1;}
		 if (ch==70)
			{
			if (cb>70) {
				    cout<<"Here is your candy bar\n";
				    cb-=70;
				   }
				else cout<<"Error:Deposit more money\n";
			ch=-1;}
		 if (ch==85)
			{
			if (cb>85) {
				    cout<<"Here are your chips\n";
				    cb-=85;
				   }
				else cout<<"Error:Deposit more money\n";
			ch=-1;}
		read();
       }
       cout << "GoodBye\n";
}

:cheesy:

Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

Quite nice,Fili

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks Fire Net :D Do you suppose it's too late for it to be of any use?

Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

I dont think so,it will be of some use to someone,hey teachers usally accept a submission even if it a bit late.

Member Avatar
MaxC
Newbie Poster
11 posts since May 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I dont think so,it will be of some use to someone

Thank you :cheesy:

Member Avatar
MtBobcat
Newbie Poster
3 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm not expert but I did programming in my 2 year course just finished and understood that vending machines work on the weight of coins.

:rolleyes:

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm not expert but I did programming in my 2 year course just finished and understood that vending machines work on the weight of coins.

:rolleyes:

Well, that's what his problem said. Anyway i'm not a US or UK citizen so i'm not great at english :sad: What is a vending machine exactly?! :o

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

>What is a vending machine exactly?!

Something like this.

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

ahh ok I see. Thanks Dave :)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article