ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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 (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;
// 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
// 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
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;
}
first I would clean up the switch statement just so it's more readable:
Code:
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
while (sel!=5)
{
switch (sel)
{
case '1':
GetQuantity(DESCR_TB, quanTb);
break;
case '2':
GetQuantity(DESCR_SD, quanSd);
break;
case '3':
GetQuantity(DESCR_SS, quanSs);
break;
case '4':
GetQuantity(DESCR_BW, quanBw);
break;
}
ClearScreen();
sel = GetMenuSelection(Tb, Sd, Ss, Bw);
cout << "user selected: " << sel << endl;
// That stmt is what scrolls during execution
}
then I would fix my while loop. unless you typed in your code wrong sel is a char so to say
Code:
while (sel != 5)
is wrong, it should be
Code:
while (sel != '5')
. hope that fixes you up.
jpbarto
(now I go back to the wonderful world of term papers and facial recognition... ugh)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.