Mar 2, 2015 at 8:10pm Mar 2, 2015 at 8:10pm UTC
In this program the user is supposed to choose a menu item, then the program is supposed to run a specific function based on their choice. Problem is I can't get the program to run the function after the choice is made. It just keeps looping. Can anyone help me out?
Note: I am not to use pass by reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include <iostream>
#include <iomanip>
using namespace std;
//Global Variables
enum transType { SCAN_ITEM = 1, DISCOUNT, RECEIPT, EXIT};
const double TAX = 0.0825;
int main ()
{
cout.precision (2);
double itemPrice = 0.0, totalCost=0.0;
//initialize function variables
int choice, menuSelection;
double price, runningTotal, givenDiscount;
//initialize functions
double getDiscountRate(double discountRate);
double getPrice(double itemPrice, double totalCost);
void printReceipt(double totalCost, double discountRate);
int showMenu(int choice);
do {
showMenu(menuSelection);
switch (menuSelection)
{
case SCAN_ITEM:
getPrice (price, runningTotal);
break ;
case DISCOUNT:
getDiscountRate(givenDiscount);
break ;
case RECEIPT:
printReceipt(runningTotal, givenDiscount);
break ;
case EXIT:
printReceipt(runningTotal, givenDiscount);
cout << "System Exit\n" << endl;
break ;
}
} while (menuSelection != EXIT);
system ("pause" );
return 0;
}
//Functions
int showMenu(int choice)
{
cout << "Supermarket POS System\n"
<< "----------------------------------\n"
<< "Select an option:\n"
<< SCAN_ITEM << ". Scan an Item\n"
<< DISCOUNT << ". Set Discount Rate\n"
<< RECEIPT << ". Print Receipt\n"
<< EXIT << ". Exit\n"
<< "\n >>" ;
cin >> choice;
return (choice);
}
double getPrice(double itemPrice, double totalCost)
{
do {
cout << "Enter the item price (positive number only): " ;
cin >> itemPrice;
if (itemPrice <= 0) {
cout << "Please enter a positive number only.\n" << endl;
}
else {
totalCost += itemPrice;
cout << "Running Total: " << fixed << totalCost << "\n" << endl;
}
} while (itemPrice <= 0);
return (totalCost);
}
double getDiscountRate(double discountRate)
{
cout << "Enter the Discount Rate (0.0-1.0): " ;
cin >> discountRate;
cout << "Customer discount is at " << discountRate*100 <<"% off.\n" << endl;
return (discountRate);
}
void printReceipt(double totalCost, double discountRate)
{
double discountTotal, taxTotal;
discountTotal = totalCost*discountRate;
taxTotal = (totalCost-discountTotal)*TAX;
cout << "Total Purchase:\t " << setw(8) << totalCost << "\n" ;
if (discountRate > 0) {
cout << "Discount:\t" << "-" << setw(8) << discountTotal << "\n" ;
}
cout << "Tax:\t\t" << "+" << setw(8) << taxTotal << "\n" ;
cout << "Total Charge:\t" << "=" << setw(8) <<totalCost - discountTotal + taxTotal << "\n" << endl;
}
Last edited on Mar 3, 2015 at 1:38am Mar 3, 2015 at 1:38am UTC
Mar 2, 2015 at 8:14pm Mar 2, 2015 at 8:14pm UTC
You're not saving the data returned from "showMenu()", or from any other functions for that matter, anywhere. Nor are you passing 'menuSelection' by reference.
Last edited on Mar 2, 2015 at 8:15pm Mar 2, 2015 at 8:15pm UTC
Mar 2, 2015 at 8:53pm Mar 2, 2015 at 8:53pm UTC
I thought I was with the return in showMenu() with return(choice)?
Sorry I'm really new to all of this, could you maybe explain a little further how I could fix this? Thanks :)
Mar 2, 2015 at 8:57pm Mar 2, 2015 at 8:57pm UTC
To add to @Computergeek01 and to answer your question @Skyllianfive
Yes, you do return them from each function, BUT you never save them.
For example, in the showMenu you return an integer.
Then doing this - showMenu(menuSelection);
Isint enough. You have to save it, in and in this case in a integer variable -
int saveMenu= showMenu(menuSelection);
And then now whatever integer the function returned is now stored in saveMenu and can be used for your switch statement -
switch (saveMenu)
Last edited on Mar 2, 2015 at 8:58pm Mar 2, 2015 at 8:58pm UTC
Mar 2, 2015 at 9:20pm Mar 2, 2015 at 9:20pm UTC
I did as you explained but it's still looping instead of running the switch statements.
Here's the updated code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
int main ()
{
cout.precision (2);
double itemPrice = 0.0, totalCost=0.0;
//initialize function variables
menuSelection;
double price, runningTotal, givenDiscount;
int saveMenu = showMenu(menuSelection);
//initialize functions
double getDiscountRate(double discountRate);
double getPrice(double itemPrice, double totalCost);
void printReceipt(double totalCost, double discountRate);
int showMenu(int choice);
do {
showMenu(menuSelection);
switch (saveMenu)
{
case SCAN_ITEM:
getPrice (price, runningTotal);
break ;
case DISCOUNT:
getDiscountRate(givenDiscount);
break ;
case RECEIPT:
printReceipt(runningTotal, givenDiscount);
break ;
case EXIT:
printReceipt(runningTotal, givenDiscount);
cout << "System Exit\n" << endl;
break ;
}
} while (saveMenu != EXIT);
system ("pause" );
return 0;
}
//Functions
int showMenu(int choice)
{
cout << "Supermarket POS System\n"
<< "----------------------------------\n"
<< "Select an option:\n"
<< SCAN_ITEM << ". Scan an Item\n"
<< DISCOUNT << ". Set Discount Rate\n"
<< RECEIPT << ". Print Receipt\n"
<< EXIT << ". Exit\n"
<< "\n >>" ;
cin >> choice;
return (choice);
}
double getPrice(double itemPrice, double totalCost)
{
do {
cout << "Enter the item price (positive number only): " ;
cin >> itemPrice;
if (itemPrice <= 0) {
cout << "Please enter a positive number only.\n" << endl;
}
else {
totalCost += itemPrice;
cout << "Running Total: " << fixed << totalCost << "\n" << endl;
}
} while (itemPrice <= 0);
return (totalCost);
}
double getDiscountRate(double discountRate)
{
cout << "Enter the Discount Rate (0.0-1.0): " ;
cin >> discountRate;
cout << "Customer discount is at " << discountRate*100 <<"% off.\n" << endl;
return (discountRate);
}
void printReceipt(double totalCost, double discountRate)
{
double discountTotal, taxTotal;
discountTotal = totalCost*discountRate;
taxTotal = (totalCost-discountTotal)*TAX;
cout << "Total Purchase:\t " << setw(8) << totalCost << "\n" ;
if (discountRate > 0) {
cout << "Discount:\t" << "-" << setw(8) << discountTotal << "\n" ;
}
cout << "Tax:\t\t" << "+" << setw(8) << taxTotal << "\n" ;
cout << "Total Charge:\t" << "=" << setw(8) <<totalCost - discountTotal + taxTotal << "\n" << endl;
}
Last edited on Mar 2, 2015 at 9:35pm Mar 2, 2015 at 9:35pm UTC
Mar 2, 2015 at 9:39pm Mar 2, 2015 at 9:39pm UTC
Thats kinda not what I said. Just do this between your Do loop and switch statement, Nothing else -
int saveMenu= showMenu(menuSelection);
This will save the integer and display the menu.
1 2
//initialize function variables
menuSelection;
No idea what that is suppose to be. You're not initializing anything. So you can remove that one.
Also remove this -
int saveMenu = showMenu(menuSelection);
Edit: Where you initialize variables. Create "int choice = 0;" And send that one in instead of menuSelection, so it looks like this -
int saveMenu= showMenu(choice)
Last edited on Mar 2, 2015 at 9:40pm Mar 2, 2015 at 9:40pm UTC
Mar 2, 2015 at 10:26pm Mar 2, 2015 at 10:26pm UTC
Now it's telling me that saveMenu is undeclared in my while statement and the loop won't break when I put in exit.
Last edited on Mar 2, 2015 at 11:40pm Mar 2, 2015 at 11:40pm UTC
Mar 2, 2015 at 11:39pm Mar 2, 2015 at 11:39pm UTC
Uh, I did put it inside the do brackets, if you read my post I said the problem is my while statement. The loop will not break unless I input the EXIT choice twice.
Mar 2, 2015 at 11:45pm Mar 2, 2015 at 11:45pm UTC
Sorry about that. Just use a boolean. You can create a
Then run the loop
while (ok != false )
Then you can set "ok" to false in one of the cases, Im guessing the exit one.
Or you could just use return 0;
Also, you dont want the system("pause"); one there. You want the program to shut down instantly after the loops breaks, not wait for further input.
Last edited on Mar 2, 2015 at 11:48pm Mar 2, 2015 at 11:48pm UTC
Mar 2, 2015 at 11:51pm Mar 2, 2015 at 11:51pm UTC
Got it, thanks for all the help and for all your patience. :)
Mar 2, 2015 at 11:56pm Mar 2, 2015 at 11:56pm UTC
Glad to help :) goodluck to you.
Mar 3, 2015 at 9:05am Mar 3, 2015 at 9:05am UTC
So I got points docked off for using the wrong boolean loop termination. It says I should've put (saveMenu != EXIT). Problem is that whenever I try to run the program after applying that fix the program won't even compile because saveMenu is not defined outside outside of the do brackets. Then when I try to run it after defining saveMenu outside of it my program just crashes.
Any ideas?