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

Dani AI

Generated

Nice start, . Building on ’s advice to keep everything in the smallest unit, store your balance as an int number of cents and work only with integers. Two gotchas I see in code like ’s draft: (1) when checking if a purchase is allowed, use >= (exact payment should vend), and (2) do not gate coin returns on divisibility like cb % 25 == 0. That logic skips quarters for amounts like 65 and falls back to lots of nickels. Instead, use a simple greedy pass over denominations, subtracting as you go. For US coins {100, 50, 25, 10, 5}, the greedy algorithm is optimal, so you will always get the correct breakdown.

Here is a focused change-making helper you can drop in. It prints nothing for zero-count coins and works for any nonnegative balance:

void give_change(int cents) {
    const int denom[] = {100, 50, 25, 10, 5};
    const char* name[] = {"dollar", "half dollar", "quarter", "dime", "nickel"};
    for (int i = 0; i < 5; ++i) {
        int count = cents / denom[i];
        cents %= denom[i];
        if (count > 0) {
            std::cout << "Returning " << count << " " << name[i]
                      << (count > 1 ? "s" : "") << "\n";
        }
    }
}

A few quick cleanups that make debugging easier:

  • Use a switch for the menu; put invalid entries in default so you can say "Invalid option."
  • Keep a const price table (e.g., gum=45, chips=85). When a user selects an item: if balance >= price, subtract and vend; otherwise prompt to deposit more.
  • Prefer int main() and #include <iostream> (avoid nonstandard headers and clrscr()), which will compile cleanly on modern compilers.

Recommended Answers

All 11 Replies

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

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

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:

Quite nice,Fili

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

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

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

Thank you :cheesy:

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:

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

>What is a vending machine exactly?!

Something like .

ahh ok I see. Thanks Dave :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.