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;

}

Recommended Answers

All 2 Replies

Your do...while loop never ends, because endingcoupon is always 1.

You need to reexamine what your process is - I can't really follow what you're trying to do. If you redeem coupons, something has to be reduced so that further looping starts from a lower value.

Your do...while loop never ends, because endingcoupon is always 1.

You need to reexamine what your process is - I can't really follow what you're trying to do. If you redeem coupons, something has to be reduced so that further looping starts from a lower value.

Thank You. I got the issue solved. You can check it over if you want.

// 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;
      int newcoupon;
      int totalcoupons;

      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 now have " << endcoupon << " coupons!" << endl << endl;

      endingcoupon = endcoupon - 7;

      endingbar = endingcoupon;

	if ( endingbar >= 1){

      cout << "You get " << endingbar << " additional bar(s)." << endl << endl;
  
      totalbars = bars + freebars + endingbar;
 
      cout << "Total amount of candy bars you have so far is " << totalbars << endl << endl;

	}

	else if ( endingbar < 1 ){

	totalbars = bars + freebars;

	cout << "The total amount of candy bars you have is " << totalbars << endl << endl;
 
	}
  
      }while(endingcoupon >= 7);
  
      while(endingcoupon < 7){
 
      cout << "You do not have enough coupons to redeem a free candy bar." << endl;
	
	  cout << endl;
      
	  break;
  
      }
  
}

    // 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;

      }
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.