mainMenu keeps on appearing at the end of every option

A

Ashliey Elx

Hi, my program codes has no error but my mainMenu keeps on appearing at the end of every option I choose.
The last command of Food Menu is to ask user whether they want to add another food or not but when I key in Y/y, my mainMenu appear instead of my Food Menu, the same goes with my Drink Menu.
Anyone of you know what's wrong with my codes?

Header File:
void mainMenu();
void foodMenu();
void drinkMenu();
void totalPayment();
void exit();

Menu File:
#include "Header.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int option = 0;
char add;
const int SIZE = 20;
string foodCode;
string drinkCode;
int foodQty;
int drinkQty;
float foodPrice;
float drinkPrice;
float totalFood;
float totalDrink;
float totalPrice;


void mainMenu() {
int option=0;

do {
cout << endl
<< setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "| ###### ###### ###### ##### |" << endl;
cout << setw(65) << "| # # # # |" << endl;
cout << setw(65) << "| ###### ###### # ### # |" << endl;
cout << setw(65) << "| # # # # # |" << endl;
cout << setw(65) << "| ###### ###### ###### ##### |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| SEGi College Canteen System |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| 1. Food Menu |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| 2. Drink Menu |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| 3. Total Payment |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| 4. Exit |" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl
<< endl;

cout << setw(45) << "Enter Your Option : ";
cin >> option;
cin.ignore(1,'\n');
cout << endl;

switch(option)
{
case 1:
foodMenu();
break;

case 2:
drinkMenu();
break;

case 3:
totalPayment();
break;

default:
cout << "Please enter a number between range 1-4!" << endl;
cout << "Enter Your option: ";
cin.ignore(1,'\n');
cin >> (option);
cin.ignore(1,'\n');
}
}while(option !=4);
}


void foodMenu() {
bool menu1 = true;
do {
char add;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "| SEGi College Canteen System |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| [ FOOD MENU ] |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| FOOD CODE PRICE |" << endl;
cout << setw(65) << "| F01 RICE RM1.00 |" << endl;
cout << setw(65) << "| F02 CURRY CHICKEN RM3.00 |" << endl;
cout << setw(65) << "| F03 FRIED CHICKEN RM3.00 |" << endl;
cout << setw(65) << "| F04 FRIED FISH RM2.00 |" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;

cout << setw(45) << "Enter Food Code : ";
cin >> foodCode;
cout << endl
<< setw(47) << "Enter Food Quantity : ";
cin >> foodQty;

cout << "Do you want to add another food[Y/N]: ";
cin >> add;

while (add != 'Y' && add != 'N' && add != 'y' && add != 'n'){
cout << "Please enter Y/N:";
cin >> add;
}
if (add == 'N' || add == 'n'){
mainMenu();
}
}while (add == 'Y' || add == 'y');
}

void drinkMenu() {
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "| SEGi College Canteen System |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| [ DRINK MENU ] |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| DRINK CODE PRICE |" << endl;
cout << setw(65) << "| D01 MILO RM1.80 |" << endl;
cout << setw(65) << "| D02 TEH-O RM0.90 |" << endl;
cout << setw(65) << "| D03 100 PLUS RM2.50 |" << endl;
cout << setw(65) << "| D04 APPLE JUICE RM2.00 |" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;

cout << setw(45) << "Enter Drink Code : ";
cin >> drinkCode;
cout << endl
<< setw(47) << "Enter Drink Quantity : ";
cin >> drinkQty;
}

void totalPayment() {
}

void exit () {
}

int main () {
mainMenu();
system("pause");
return 0;

}
 
O

Osmium

Ashliey Elx said:
Hi, my program codes has no error but my mainMenu keeps on appearing at
the end of every option I choose.
The last command of Food Menu is to ask user whether they want to add
another food or not but when I key in Y/y, my mainMenu appear instead of
my Food Menu, the same goes with my Drink Menu.
Anyone of you know what's wrong with my codes?

Header File:
void mainMenu();
void foodMenu();
void drinkMenu();
void totalPayment();
void exit();

Menu File:
#include "Header.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int option = 0;
char add;
const int SIZE = 20;
string foodCode;
string drinkCode;
int foodQty;
int drinkQty;
float foodPrice;
float drinkPrice;
float totalFood;
float totalDrink;
float totalPrice;


void mainMenu() {
int option=0;

do {
cout << endl
<< setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" <<
endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "| ###### ###### ###### #####
|" << endl;
cout << setw(65) << "| # # # #
|" << endl;
cout << setw(65) << "| ###### ###### # ### #
|" << endl;
cout << setw(65) << "| # # # # #
|" << endl;
cout << setw(65) << "| ###### ###### ###### #####
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| SEGi College Canteen System
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| 1. Food Menu
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| 2. Drink Menu
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| 3. Total Payment
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| 4. Exit
|" << endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - *
*" << endl
<< endl;

cout << setw(45) << "Enter Your Option : ";
cin >> option;
cin.ignore(1,'\n');
cout << endl;

switch(option)
{
case 1:
foodMenu();
break;

case 2:
drinkMenu();
break;

case 3:
totalPayment();
break;

default:
cout << "Please enter a number between range 1-4!" << endl;
cout << "Enter Your option: ";
cin.ignore(1,'\n');
cin >> (option);
cin.ignore(1,'\n');
}
}while(option !=4);
}


void foodMenu() {
bool menu1 = true;
do {
char add;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - *
*" << endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "| SEGi College Canteen System
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| [ FOOD MENU ]
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| FOOD CODE PRICE
|" << endl;
cout << setw(65) << "| F01 RICE RM1.00
|" << endl;
cout << setw(65) << "| F02 CURRY CHICKEN RM3.00
|" << endl;
cout << setw(65) << "| F03 FRIED CHICKEN RM3.00
|" << endl;
cout << setw(65) << "| F04 FRIED FISH RM2.00
|" << endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - *
*" << endl;

cout << setw(45) << "Enter Food Code : ";
cin >> foodCode;
cout << endl
<< setw(47) << "Enter Food Quantity : ";
cin >> foodQty;

cout << "Do you want to add another food[Y/N]: ";
cin >> add;

while (add != 'Y' && add != 'N' && add != 'y' && add != 'n'){
cout << "Please enter Y/N:";

Look up "short-circuit evalutaion".
cin >> add;
}
if (add == 'N' || add == 'n'){
mainMenu();
}
}while (add == 'Y' || add == 'y');
}

void drinkMenu() {
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - *
*" << endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "| SEGi College Canteen System
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| [ DRINK MENU ]
|" << endl;
cout << setw(65) << "|
|" << endl;
cout << setw(65) << "| DRINK CODE PRICE
|" << endl;
cout << setw(65) << "| D01 MILO RM1.80
|" << endl;
cout << setw(65) << "| D02 TEH-O RM0.90
|" << endl;
cout << setw(65) << "| D03 100 PLUS RM2.50
|" << endl;
cout << setw(65) << "| D04 APPLE JUICE RM2.00
|" << endl;
cout << setw(65) << "*
*" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - *
*" << endl;

cout << setw(45) << "Enter Drink Code : ";
cin >> drinkCode;
cout << endl
<< setw(47) << "Enter Drink Quantity : ";
cin >> drinkQty;
}

void totalPayment() {
}

void exit () {
}

int main () {
mainMenu();
system("pause");
return 0;

}
 
S

Stefan Ram

David Harmon said:

The actual reason for »Yuck!« should be that the code was
not /trimmed down/ to the smallest possible program showing
the problem (to an SSCCE).

Another »Yuck!« for one poster who quoted all this code to
add a single line hidden somewhere.

Another »Yuck!« possibly for lack of ideas how to debug
one's code. But for beginners this lack of debugging
capabilities is acceptable, because when one is still
learning, one does not have to know everything.
Do not use setw() for no reason whatsoever.

Why not use setw to set a width, if this should be required?
Do not use endl when "\n" will do

Recently I read someone who recommended to always use
»::std::endl«, because this will make the last output
visible should the program crash before the next other
flushing operation, so it should help debugging.
Do not use many operator<< when all you need is one.
cout << "\n"
"* * - - - - - - - - - - - - - - - - - - - - - - - * *\n"
"* *\n"
"| ###### ###### ###### ##### |\n"
"| # # # # |\n"
"| ###### ###### # ### # |\n"

Or even: ::std::cout << R("
* * - - - - - - - - - - - - - - - - - - - - - - - * *
* *
| ###### ###### ###### ##### |
| # # # # |
| ###### ###### # ### # |
....

However, when preparing an SSCCE, all of this »* * - - ...«
would be omitted anyways.
 
I

Ike Naar

Hi, my program codes has no error but my mainMenu keeps on appearing
at the end of every option I choose.
The last command of Food Menu is to ask user whether they want to add
another food or not but when I key in Y/y, my mainMenu appear instead
of my Food Menu, the same goes with my Drink Menu.
Anyone of you know what's wrong with my codes?

Header File:
void mainMenu();
void foodMenu();
void drinkMenu();
void totalPayment();
void exit();

Menu File:
#include "Header.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int option = 0;
char add;

Note this global declaration of char 'add'.

[...]
void foodMenu() {
bool menu1 = true;
do {
char add;

Note this local declaration of char 'add'.
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "| SEGi College Canteen System |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| [ FOOD MENU ] |" << endl;
cout << setw(65) << "| |" << endl;
cout << setw(65) << "| FOOD CODE PRICE |" << endl;
cout << setw(65) << "| F01 RICE RM1.00 |" << endl;
cout << setw(65) << "| F02 CURRY CHICKEN RM3.00 |" << endl;
cout << setw(65) << "| F03 FRIED CHICKEN RM3.00 |" << endl;
cout << setw(65) << "| F04 FRIED FISH RM2.00 |" << endl;
cout << setw(65) << "* *" << endl;
cout << setw(65) << "* * - - - - - - - - - - - - - - - - - - - - - - - * *" << endl;

cout << setw(45) << "Enter Food Code : ";
cin >> foodCode;
cout << endl
<< setw(47) << "Enter Food Quantity : ";
cin >> foodQty;

cout << "Do you want to add another food[Y/N]: ";
cin >> add;

while (add != 'Y' && add != 'N' && add != 'y' && add != 'n'){
cout << "Please enter Y/N:";
cin >> add;
}
if (add == 'N' || add == 'n'){
mainMenu();
}
}while (add == 'Y' || add == 'y');

The scope of the local 'add' variable ends at the '}' just before the 'while'
keyword.
The 'add' variable mentioned in the while clause is the global 'add',
not the local 'add'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top