Hey guys, I have a soda machine program assignment that requires the user to insert an amount that has to equal to dollars, quarters, dimes, or nickels. Any other input will be displayed in the "Coin Return". The display should loop showing the current amount deposited until the user's deposit is greater than or equal to .75. An entry of "-1" will turn off the machine. My question is when I input .25 three times (which is .75) it breaks out of the loop and doesn't update the slot amount total. Also, when I enter a number larger than what it will take (dollars,quarters,etc.), let's say 25, instead of adding it to the Coin Return, it doesn't accept it and just refreshes. My teacher told me I can use a boolean, but I don't know exactly where or how to do it. Other than that, I am basically finished with the program. If anyone can please help me, I would appreciate it a lot. Here's what I have so far:

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int ichange, quarters, dimes, nickels, pennies;
    double insert, dchange, CoinReturn=0.00, total=0.00;
    bool flag=true;
    
    do
    {
      CoinReturn=0.00;
      total=0.00;
      do
      {
         system("cls");
         cout<<endl;
         cout<<"                   ICE COLD COLA "<<endl;
         cout<<"                   Only .75 cents!               "<<endl<<endl;
    
         cout<<"   Deposit -- dollar, quarters, dimes, or nickels:"<<endl;
         cout<<"              ( 1   ,    .25  ,  .10 ,     .05  )"<<endl<<endl;
    
    
        cout<<"  Slot" <<": ["<< total <<"]"<<endl<<endl;
        cout<<"  Coin Return"<<": ["<< CoinReturn <<"]"<<endl<<endl;             
        cout<<"  Enter coin [------] ";
        cin>> insert;
        cout<<endl<<endl;
        
        if(insert!=1 && insert!=.25 && insert!=.10 && insert!=.05)
           CoinReturn=insert;   
        else
           if(flag)  
           total+=insert; 
           
        if(total>=.75)
        {
           cout<<"   Here is your Cola!"<<endl<<endl<<endl;
           cout<<"   Have a nice day!"<<endl;
           break;
        }     
        
      }while(insert<=.75 && insert!=-1);
      
      dchange=total-.75;
      
      while(dchange>0)
      {
       ichange = static_cast<int>((dchange +.005) * 100);
    
       quarters = ichange/25;
       ichange = ichange%25;
    
       dimes = ichange/10;
       ichange = ichange%10;
    
       nickels = ichange/5;
       ichange = ichange%5;
    
       pennies = ichange/1;
    
       cout<< endl<< endl<< "  Your change is: " <<endl;
       cout<<"\t"<< quarters <<" Quarter(s)"<<endl;
       cout<<"\t"<< dimes <<" Dime(s)"<<endl;
       cout<<"\t"<< nickels<< " Nickel(s)"<<endl;
       cout<<"\t"<< pennies <<" Penny(ies)"<<endl<<endl;
       break;
      }
      
      system("pause");

      
    }while(insert != -1);   
    
    cout<<endl<<endl;
    
}

My question is when I input .25 three times (which is .75) it breaks out of the loop and doesn't update the slot amount total.

It breaks out of the loop because of your if statement:

if(total>=.75)
			{
				cout<<"   Here is your Cola!"<<endl<<endl<<endl;
				cout<<"   Have a nice day!"<<endl;
				break;
			}

So if you enter 3x .025 you enter the if statement, print out the message and then break out of the loop with break;
Same if your input is 1, it doesn't update the slot ammount total and skips directly to the change part.

Also, when I enter a number larger than what it will take (dollars,quarters,etc.), let's say 25, instead of adding it to the Coin Return, it doesn't accept it and just refreshes.

Now about this I'm not so sure. Looking at the code I don't see why it would do that right away.. I don't really like do-while loops either so never really used them. (This is all possible using a for or a while). Hopefully someone can help you with this.

On a side note I would also change the section where you calculate the change to an IF without the need to use a while and break;

Also your CoinReturn isn't adding up but just resetting everytime you enter a new value. So if you enter .15 once and then .15 again, the CoinReturn still is .15 and not .30.

And finally the boolean expression 'flag' seems useless to me. Just remove the IF in:

else
				if(flag)  
					total+=insert;

And it should work fine. (Also remove the declaration)

There are some other things that could be better in your program but I understand you are just starting. I'm myself not really an advanced user.

Hope this helps!

I tried your advice and unfortunately, it didn't work.. I really appreciate your help though.

Also your CoinReturn isn't adding up but just resetting everytime you enter a new value. So if you enter .15 once and then .15 again, the CoinReturn still is .15 and not .30.

..and this isn't a problem because my teacher's executable did the same thing so he won't count off for it. I just really need help on the other problems..

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.