I'm having some trouble with my while loop in my code. If I enter in 20 and then enter in 14 for the coupons I want to redeem, it goes into a constant loop and doesn't stop. However I did stop it from doing this by putting in a break;
. While this may work it also causes some issues. Some times it runs the whole program properly where the loop will run until it see's that there aren't enough coupons and it tells you that or it will stop and won't get to that point.
How do I fix this? I want the loop to run through and then stop like it should no matter what number I enter. Maybe this is very simple and I'm just not seeing it. But it's 12am here in NJ and I'm beat, it's the one and only thing that's giving me trouble. If that small qwerk get's solved I can turn it in tomorrow :)
// candybar.cpp : Defines the entry point for the console application.
// David Tarantula
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int dollars = 0;
int coupons = 0;
int redeem = 0;
int bars = 0;
int freebars = 0;
int endcoupon = 0;
int endingcoupon = 0;
int endingbar = 0;
int totalbars = 0;
int newcoupon;
int totalcoupons = 0;
cout << "Please enter dollar bills." << endl;
cin >> dollars;
cout << endl;
bars = dollars;
coupons = bars;
cout << "You have " << bars << " chocolate bars and " << coupons << " coupons" << endl;
cout << endl;
cout << "7 Coupons = 1 Free Bar. How many would you like to redeem?: " << endl;
cin >> redeem;
cout << endl;
// Runs and calculates if Coupons are redeemable
if(redeem >= 7) {
cout <<"You got " << bars << " Candy Bars! " << endl << endl;
cout << "You originally had " << coupons << " coupons!" << endl << endl;
newcoupon = coupons - redeem;
cout << "You now have " << newcoupon << " more coupons!" << endl << endl;
// More coupons are redeemable the following runs.
do{
totalcoupons = redeem/7;
freebars = totalcoupons;
cout << "You get " << freebars << " free bars." << endl << endl;
endcoupon = newcoupon + totalcoupons;
cout << "You have " << endcoupon << " coupons!" << endl << endl;
endingcoupon = endcoupon - 7;
endingbar = endingcoupon;
cout << "You get " << endingbar << " additional bar(s)." << endl << endl;
totalbars = bars + freebars + endingbar;
cout << "Total amount of candy bars you have is " << totalbars << endl << endl;
break;
}while(endingcoupon < 7);
while(endingcoupon < 7){
cout << "You do not have enough coupons to redeem anymore." << endl;
break;
cout << endl;
}
}
// Displays if there are no coupons redeemable from the start
else if (redeem < 7) {
cout << "Error: You do not have enough coupons for a free Candy Bar!" << endl << endl;
cout << "You only get " << bars << " candy bars." << endl;
cout << endl;
}
system("pause");
return 0;
}