Stopping infinite loop

Hi there. New to the site and to programming in general. I'm currently a student and we are doing a project for C++. I have modified my instructors code and now have a loop occuring that seems to be infinite when the program is run. Showing code below.

/* Inputs: Number of scoops, toppings (c=chocolate sauce, s=sprinkles)
Ouputs: total price of ice cream
Formulas: 3 scoops are $5.00, 2 scoops are $4.00, 1 scoop is $2.00
Chocolate sauce is $1.00 extra, sprinkles are $1.50 extra.


Purpose: Calculates the total amount owed for an order of ice cream and prints
the ice cream options.
*/

// Identify libraries and objects used in this program, for input/output library.
#include <iostream>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;
using std::ios;

// Define constant values used in formulas
const double SPRINKLES = 1.50;
const double CHOCOLATESAUCE = 1.00;
const double THREESCOOPS = 7.00;
const double TWOSCOOPS = 5.00;
const double ONESCOOP = 4.50;
const double VANILLA = 0.00;
const double STRAWBERRY = 1.00;

// ======================
// main function
// ======================
int main()
{
//
// Variable declarations
bool errorDetected = false; // Boolean variable flagging an error
char keepGoing = 'Y'; // Character variable to decide when to stop
char topping; // to hold the value of the topping requested // to show the type of ice cream requested
int scoopSize; // 3, 2 OR 1 SCOOP
double toppingPrice; // additional price if a topping is added
double flavor; // additional cost for different flavors of ice cream
double scoopPrice = 0.00; // amount owed for one pizza purchased
double totalOwed = 0.00; // amount owed for entire order

// Clear the display screen and display a welcome message
system("clear");
cout << endl << "Welcome to the Ice Cream Parlor!" << endl << endl;

// Loop until the user indicates that (s)he is finished ordering
while (keepGoing == 'Y'||keepGoing == 'y')
{
// Get Input Data
cout << "How many scoops would you like (3, 2 or 1) in your bowl? ";
cin >> scoopSize;

cout << "\nWould you like Vanilla (V) or Strawberry (B) Ice Cream? ";
cin >> flavor;

cout << "\nWould you like any Chocolate Sauce (C) or some Sprinkles (S)? ";
cin >> topping;


// Test the scoops to determine what price to charge. *** Notice the
// use of == operator. DO NOT use a single = sign!
if (scoopSize == 3)
scoopPrice = THREESCOOPS;
else if (scoopSize == 2)
scoopPrice = TWOSCOOPS;
else if (scoopSize == 1)
scoopPrice = ONESCOOP;
else
{
cout << "Error in your scoop size, please choose 3, 2 or 1.\n\n";
errorDetected = true;

}
// Set the output object to show decimals as 2 pt fixed, e.g. 999.99
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

// If input was OK (no error detected), then continue with the rest of the calculations.
if (!errorDetected)
{
if (topping == 'S'|| topping == 's') // Check for sprinkles in upper or lower case letters
scoopPrice = scoopPrice + SPRINKLES;
if (topping == 'S'|| topping == 's') // Check for chocolate sauce in upper or lower case letters
scoopPrice = scoopPrice + CHOCOLATESAUCE;
if (flavor == 'B'|| flavor == 'b') // Check for strawberry in upper or lower case letters
scoopPrice = scoopPrice + STRAWBERRY;

// Display the amount owed for 1 bowl
cout << "\nYou owe $" << scoopPrice << " for this bowl.\n";
// Add the amount owed to the total owed
totalOwed = totalOwed + scoopPrice;

}
cout << "Would you like to order more ice cream (y/n)? ";
cin >> keepGoing;
} // return to top of loop

// Display the final total and goodbye message
cout << "\nYou owe $" << totalOwed << " for your order.\n";
cout << "Thanks for your order. We appreciate your business!\n\n";
system("pause");

return 0; // return control to the operating system using a SUCCESS code
}
// ------------------------------------------
// --------- END OF MAIN PROGRAM-------------
// ------------------------------------------
Last edited on
1
2
cout << "\nWould you like Vanilla (V) or Strawberry (B) Ice Cream? ";
cin >> flavor;

flavor should be char if you want to store 'V' or 'B' in it.

You also need to remove a dangling '\n'
1
2
3
cout << "Would you like to order more ice cream (y/n)? ";
cin.ignore(256, '\n');
cin >> keepGoing;


With these fixes it seems to work.


'clear' is not recognized as an internal or external command,
operable program or batch file.

Welcome to the Ice Cream Parlor!

How many scoops would you like (3, 2 or 1) in your bowl? 2

Would you like Vanilla (V) or Strawberry (B) Ice Cream? v

Would you like any Chocolate Sauce (C) or some Sprinkles (S)? c

You owe $5.00 for this bowl.
Would you like to order more ice cream (y/n)? y
How many scoops would you like (3, 2 or 1) in your bowl? 3

Would you like Vanilla (V) or Strawberry (B) Ice Cream? b

Would you like any Chocolate Sauce (C) or some Sprinkles (S)? s

You owe $10.50 for this bowl.
Would you like to order more ice cream (y/n)?


BTW system("clear") does not work on Windows. A alternative - untested - is
1
2
3
4
5
6
7
8
void clrscr()
{
#if defined(WIN32) || defined(WIN64)
  system("cls");
#else
  system("clear");
#endif
}
I'll try those changes right now. Thank you so very much!
Topic archived. No new replies allowed.