LQ Newbie
Registered: Dec 2004
Posts: 16
Rep:
|
Another ? from a newbie - stuck in a while loop
For some reason I am stuck in this while loop. I know that I exit the switch, because I put a dummy cout stmt in there and checked, and that cout just goes crazy scrolling up the page. I never get to the dummy cout that says I exited the while loop. If I remove the cout that says I exited the switch, the screen just seems to hang empty during execution. The code compiles just fine, but it certainly isn't running correctly. Here is the abbreviated code bit for this problem, I'll post the entire code below it.
Any ideas?
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
while (sel!=5)
{
switch (sel)
{
case '1': GetQuantity(DESCR_TB, quanTb);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '2': GetQuantity(DESCR_SD, quanSd);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '3': GetQuantity(DESCR_SS, quanSs);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '4': GetQuantity(DESCR_BW, quanBw);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
}
cout << "you are out of switch";
// That stmt is what scrolls during execution
}
cout << "You made it out of the while";
*********And for those of you who want to read the entire code (sorry, it isn't finished yet, I'm converting some older code for a new assignment)
// CSCI 130 Section 02
// P7
// This program uses file supplied data and user interaction to create
// a receipt for a sale at the Yuppie Suppie Resteraunt. The receipt
// detail is stored in an output file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// function declarations
float CalcFoodTotal(int quan,float itemCost);
void PrintDetailLine(string descr ,int quan, float itemCost,float itemTotal);
float TaxAdded(float RATE, float costSubTotal);
float SubTotal(float total1, float total2, float total3, float total4);
float FinalTotal(float subtotal, float tax);
void PrintFinalDetail(float sub, float RATE, float tax, float finTotal);
void GetCurrentPrices (float& Tb, float& Sd, float& Ss, float& Bw);
char GetMenuSelection (float Tb, float Sd, float Ss, float Bw);
void PrintMenu(float Tb, float Sd, float Ss, float Bw);
void ClearScreen(void);
void GetQuantity(string descr, int& qty);
// constant declarations
string DESCR_TB = "Tofu Burgers ", // Provides the item description
DESCR_SD = "Sushi Dishes ", // that appears on the receipt
DESCR_SS = "Seaweed Salads",
DESCR_BW = "Bottled Waters";
float RATE = 0.06; // Specifies the tax rate
int main(void)
{
ifstream fin; // declares the file streams to be used
ofstream fout;
float Tb, // The current price of one Tofu Burger,
Sd, // Sushi Dish,
Ss, // Seaweed Salad,
Bw; // Bottled Water
float itemTotalTb, // the total amount owed for Tofu Burgers,
itemTotalSd, // Sushi Dishes,
itemTotalSs, // Seaweed Salads,
itemTotalBw; // Bottled Waters
float costSubTotal; // The cost subtotal of all items purchased
float tax; // The tax due on the subtotal
float finTotal; // The final amount due after tax is added
int quanTb, // Quantity of Tofu Burgers Purchased,
quanSd, // Sushi Dishes,
quanSs, // Seaweed Salads
quanBw; // Bottled Waters
char sel; // The menu slection choice
costSubTotal = 0; // assigns default value
quanTb = quanSd = quanSs = quanBw =0; // assigns default values of 0
cout << endl << endl;
cout << "Welcome to the Yuppie Suppie Receipt System" << endl << endl;
GetCurrentPrices(Tb, Sd, Ss, Bw); // calls function to get current
// prices from p7.in
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
while (sel!=5)
{
switch (sel)
{
case '1': GetQuantity(DESCR_TB, quanTb);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '2': GetQuantity(DESCR_SD, quanSd);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '3': GetQuantity(DESCR_SS, quanSs);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
case '4': GetQuantity(DESCR_BW, quanBw);
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
break;
}
cout << "you are out of switch";
}
cout << "You made it out of the while";
if (quanTb>0) // if item was purchased, calls CalcFoodTota
{
itemTotalTb = CalcFoodTotal(quanTb, Tb);
}
else // no item purchased, set default value 0
{
itemTotalTb = 0;
}
if (quanSd>0)
{
itemTotalSd = CalcFoodTotal(quanSd, Sd);
}
else
{
itemTotalSd = 0;
}
if (quanSs>0)
{
itemTotalSs = CalcFoodTotal(quanSs, Ss);
}
else
{
itemTotalSs = 0;
}
if (quanBw>0)
{
itemTotalBw = CalcFoodTotal(quanBw, Bw);
}
else
{
itemTotalBw = 0;
}
fout.open("p6.out", ios::app); // opens file p6.out
fout << "RECEIPT:" << endl; // inserts receipt header
fout.close(); // closes p6.out
if (itemTotalTb>0) // if item purchased, prints detail line
{
PrintDetailLine(DESCR_TB, quanTb, Tb, itemTotalTb);
}
if (itemTotalSd>0)
{
PrintDetailLine(DESCR_SD, quanSd, Sd, itemTotalSd);
}
if (itemTotalSs>0)
{
PrintDetailLine(DESCR_SS, quanSs, Ss, itemTotalSs);
}
if (itemTotalBw>0)
{
PrintDetailLine(DESCR_BW, quanBw, Bw, itemTotalBw);
}
if ((itemTotalTb>0)|| // if any item purchased, call SubTotal
(itemTotalSd>0)||
(itemTotalSs>0)||
(itemTotalBw>0))
{
costSubTotal =
SubTotal(itemTotalTb, itemTotalSd, itemTotalSs, itemTotalBw);
}
if (costSubTotal>0)
{
tax = TaxAdded(RATE, costSubTotal) + .001;
}
// if costSubTotal has a value greater than 0,
// call to TaxAdded computes the tax amount, .001 is added so that numbers
// which end in .005 are rounded up and display properly on the receipt
if (costSubTotal>0)
{
finTotal = FinalTotal(costSubTotal, tax);
}
// if costSubTotal has a value greater than 0,
// call to FinalTotal func computes the final total due including tax
PrintFinalDetail(costSubTotal, RATE, tax, finTotal);
// call to PrintFinalDetail func prints the receipt detail lines
// which include the subtotal, tax rate, tax added, and final total
cout << "Thank you, your receipt has been sent to the p7.rpt file!";
cout << endl << endl;
return 0;
}
// This function computes the amount owed for an item.
float CalcFoodTotal(int quan, float itemCost)
{
float itemTotal; // The total dollar amount owed for that # of item(s)
itemTotal = quan * itemCost;
return itemTotal;
}
// This function prints the item description, quantity, retail price, and
// total amount owed for that item to the receipt in p6.out
void PrintDetailLine(string descr, int quan, float itemCost, float itemTotal)
{
ofstream fout;
fout.open("p6.out", ios::app);
fout << showpoint << fixed << setprecision(2);
fout << descr.c_str() << setw(10) << " " << itemTotal << endl;
fout << " " << quan << " @ " << itemCost;
fout << endl;
fout.close();
return;
}
// this function calculates the subtotal for all items purchased
float SubTotal(float total1, float total2, float total3, float total4)
{
float sub; // The subtotal of all purchases added together
sub = total1 + total2 + total3 + total4;
return sub;
}
// This func calculates the tax due for all items purchased
float TaxAdded(float RATE, float costSubTotal)
{
float tax; // The amount of tax due
tax = costSubTotal * RATE;
return tax;
}
// This func calculates the total amount due after tax
float FinalTotal(float sub, float tax)
{
float finTotal; // the amount due after tax is added
finTotal = sub + tax;
return finTotal;
}
// This function prints the final detail lines to the receipt which include
// the subtotal, the tax rate, the amount of tax due, and the total amount due
void PrintFinalDetail(float sub, float RATE, float tax, float finTotal)
{
ofstream fout;
float num; // will represent the tax rate on the receipt
num = RATE * 100; // changes the rate from a decimal to a whole number
fout.open("p6.out", ios::app);
if (sub>0) // item(s) purchased, print final detail
{
fout << showpoint << fixed << setprecision(2);
fout << "subtotal" << setw(16) << " " << sub << endl;
fout << noshowpoint << fixed << setprecision(0);
fout << "tax (" << num << "\%) ";
fout << showpoint << fixed << setprecision(2);
fout << setw(15) << " " << tax << endl <<endl;
fout << "TOTAL" << setw(19) << " " << "$" << finTotal << endl;
fout << endl << endl;
}
else // no item(s) purchased, print no sale message
{
fout << "NO SALE MADE" << endl << endl;
}
fout.close();
return;
}
// This function obtains the current retail prices for the four food
// item choices from p7.in
void GetCurrentPrices (float& Tb, float& Sd, float& Ss, float& Bw)
{
ifstream fin;
fin.open("p7.in"); // open p7.in
fin >> Tb >> Sd >> Ss >> Bw; // store file data in variables
fin.close(); // close p7.in
return;
}
char GetMenuSelection (float Tb, float Sd, float Ss, float Bw)
{
char sel; // user supplied menu choice
PrintMenu(Tb, Sd, Ss, Bw);
cout << "I choose: ";
cin >> sel;
cout << endl << endl;
while ((sel<'1')||(sel>'5'))
{
ClearScreen();
cout << "****************************************" << endl;
cout << "I'm sorry, that was not a valid choice!" << endl;
cout << "You must choose from: 1, 2, 3, 4, or 5" << endl ;
cout << "****************************************" << endl << endl;
PrintMenu(Tb, Sd, Ss, Bw);
cout << "I choose: ";
cin >> sel;
cout << endl << endl;
}
return sel;
}
// This function prints the menu
void PrintMenu(float Tb, float Sd, float Ss, float Bw)
{
cout << showpoint << fixed << setprecision(2);
cout << "Please choose one of the four following menu choices" << endl;
cout << "to order that item, or press 5 to QUIT" << endl << endl;
cout << "(1) " << DESCR_TB << " $" << Tb << " each" << endl;
cout << "(2) " << DESCR_SD << " $" << Sd << " each" << endl;
cout << "(3) " << DESCR_SS << " $" << Ss << " each" << endl;
cout << "(4) " << DESCR_BW << " $" << Bw << " each" << endl;
cout << "(5) QUIT" << endl << endl;
}
// This function inserts 24 blank lines to scroll the data up the screen and
// "clear" the screen
void ClearScreen(void)
{
int i;
i = 1;
while(i<=24)
{
cout << endl;
i++;
}
}
// This function brings in the current quantity of the food item, gets
// the additional quantity, adds them together, and returns the updated
// quantity
void GetQuantity(string descr, int& qty)
{
int amount;
cout << "How many " << descr << " would you like to purchase?" << endl;
cin >> amount;
while ((qty+amount < 0)||(!cin))
{
cin.clear();
cin.ignore(256, '\n');
cout << "************************************************" << endl;
cout << "I'm sorry, the quantity you entered is not valid" << endl;
cout << "************************************************" << endl << endl;
cout << "You have chosen " << qty << " " << descr << "so far," << endl;
cout << "How many more would you like to purchase?" << endl;
cin >> amount;
}
qty = qty + amount;
return;
}
|