do-while loop with switch statements-- infinity loop error - c++

I am just trying to create a simple "menu". Basically user can input their selection and when they enter 'E', it should exit the menu. I can't catch why its giving me an infinity loop-- i know its most likely my while loop(?). its all just hard-coded as im just trying to get the gist of it.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char choice;
int numOfCups;
cout << "Hot Beverage Menu: \n";
cout << "A: Coffee $1.00 \n";
cout << "B: Tea $0.75 \n";
cout << "C: Hot Chocolate: $1.25 \n";
cout << "D: Cappuccino: $2.50 \n";
cout << "E: Exit Menu \n";
cout << "Please make a drink selection:";
cin >> choice;
do {
switch(choice) {
case 'A': cout << "You chose Coffee \n";
cout << "How many cups would you like?";
cin >> numOfCups;
cout << "Your total will be: " << '$' << fixed << setprecision(2) << (1.00 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'B': cout << "You chose Tea \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (0.75 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'C': cout << "You chose Hot Chocolate \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (1.25 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'D': cout << "You chose Cappuccino \n";
cout << "How many cups would you like? \n";
cin >> numOfCups;
cout << "Your total will be: \n" << '$' << fixed << setprecision(2) << (2.50 * numOfCups) << endl;
cout << "Please make another selection:";
cin >> choice;
break;
case 'E': cout << "Exit Menu";
break;
default: cout << "Invalid input. Please make another selection.";
break;
}
} while (choice == 'E');
return 0;
}

Loop continues as long as the condition is true, and finishes when the condition is false. Instead of while (choice == 'E') you should have while (choice != 'E').
Also, you should add cin >> choice; to the default condition, or you will have an infinite loop in that case.

Try do ... while (choice != 'E');.

Related

How can I return user to original switch menu from do-while loop?

how can I get user to go back to original switch menu once the user selects N at the end. When user selects N, would I use another loop to get them back to original menu? Any help is greatly appreciated.
cout << "Total Chips: " << chips << endl;
cout << "1) xxxxx" << endl;
cout << "2) xxx" << endl;
cout << "Please enter an option" << endl;
int option;
cin >> option;
switch(option)
{
case 1:
{
char again;
do
{
/* code
*/
cout << "Would you like to play again? Y/N" << endl;
cin >> again;
}while(towlower(again) == 'y'); // I'm not sure whether to use another do-while loop.
When user selects N, would I use another loop to get them back to original menu?
Yes, one that is put around the original menu, eg:
bool keepRunning = true;
do {
cout << "Total Chips: " << chips << endl;
cout << "1) xxxxx" << endl;
cout << "2) xxx" << endl;
cout << "Please enter an option" << endl;
int option;
cin >> option;
switch (option)
{
case 1:
{
char again;
do
{
/* code
*/
cout << "Would you like to play again? Y/N" << endl;
cin >> again;
}
while (again == 'y' || again == 'Y');
break;
}
...
}
}
while (keepRunning);

C++ codeblock Linux console menu

I am trying to build a menu on console on C++ with CodeBlock. Is for a course.
I would like to validate garbage enter by the user. For example, the user have to enter a number. If he enters a wrong number, no problem, the program work and continue. But if He enters a letter or some garbage, the program start on infinite loop.
I cannot use system(PAUSE) because I am programming on Linux.
I tried some code like cin.get() or do while with cin.get() but no result.
Here is my code :
#include <iostream>
using namespace std;
void showMenu()
{
cout << "---------------MENU --------------" << endl;
cout << "1- Check balance :" << endl;
cout << "2- Check deposit :" << endl;
cout << "3- Withdraw:" << endl;
cout << "4- Exit" << endl;
cout << "--------------------------------" << endl;
}
int main()
{
int option;
double balance = 500;
do
{
showMenu();
cout << "Option: ";
cin >> option;
cout << "\033[2J\033[1;1H";
switch(option)
{
case 1:
cout << "Balance is: " << balance << " $" << endl;
break;
case 2:
cout << "Deposit amount: " << endl;
double depositAmount;
cin >> depositAmount;
balance += depositAmount;
break;
case 3:
cout << "Withdraw amount: " << endl;
double withdrawAmount;
cin >> withdrawAmount;
if (withdrawAmount <= balance) {
balance -= withdrawAmount;
}
else {
cout << "Not enough money" << endl;
}
break;
default:
cout << "Your choice is invalid ";
do {
cout << '\n' << "Press the Enter key to continue.";
}while (cin.get() != '\n');
}
} while(option != 4);
return 0;
}
Do you have an idea how can I validate easily the garbage enter by the user ?
Thank you for your Help
It works with the code on default part : cin.clear and cin.ignore(). This last one is important. Thank you to user4581301
#include <iostream>
using namespace std;
void showMenu()
{
cout << "---------------MENU --------------" << endl;
cout << "1- Check balance :" << endl;
cout << "2- Check deposit :" << endl;
cout << "3- Withdraw:" << endl;
cout << "4- Exit" << endl;
cout << "--------------------------------" << endl;
}
int main()
{
int option;
double balance = 500;
do
{
showMenu();
cout << "Option: ";
cin >> option;
cout << "\033[2J\033[1;1H";
switch(option)
{
case 1:
cout << "Balance is: " << balance << " $" << endl;
break;
case 2:
cout << "Deposit amount: " << endl;
double depositAmount;
cin >> depositAmount;
balance += depositAmount;
break;
case 3:
cout << "Withdraw amount: " << endl;
double withdrawAmount;
cin >> withdrawAmount;
if (withdrawAmount <= balance) {
balance -= withdrawAmount;
}
else {
cout << "Not enough money" << endl;
}
break;
default:
cout << "\033[2J\033[1;1H"; //for clean screen with Linux
cout << "Your choice is invalid " << endl;
cin.clear();
cin.ignore();
}
} while(option != 4);
return 0;
}

C++ Code Help (While Loop)

#include <iostream>
using namespace std;
int main()
{
int choice;
cout << " Serendipity Booksellers\n";
cout << " Main Menu\n";
cout << "\n 1. Cashier Module";
cout << "\n 2. Inventory Database Module";
cout << "\n 3. Report Module";
cout << "\n 4. Exit\n";
cout << "\n Enter Your Choice: ";
cin >> choice;
while (choice != 4)
{
}
return 0;
Item four on the Main Menu is “Exit,” which allows the user to end the
program. Add a loop to the mainmenu.cpp program that causes it to
repeatedly display the menu until the user selects item four.
That's my assignment, can anyone help me with what to put in the while statement to accomplish that?
What do you want to do "while the choice is not 4"?
Probably you want to display the menu and ask for another choice, right?
That should already answer your question ;)
For example the loop can look like
int choice = 0;
do
{
cout << " Serendipity Booksellers\n";
cout << " Main Menu\n";
cout << "\n 1. Cashier Module";
cout << "\n 2. Inventory Database Module";
cout << "\n 3. Report Module";
cout << "\n 4. Exit\n";
cout << "\n Enter Your Choice: ";
} while ( cin >> choice && choice != 4 );
Or
do
{
cout << " Serendipity Booksellers\n";
cout << " Main Menu\n";
cout << "\n 1. Cashier Module";
cout << "\n 2. Inventory Database Module";
cout << "\n 3. Report Module";
cout << "\n 4. Exit\n";
cout << "\n Enter Your Choice: ";
if ( !( cin >> choice ) ) break;
switch ( choice )
{
case 1:
case 2;
case 3:
default:
cout << "Let's repeat one more!" << endl;
break;
case 4:
cout << "Bye!" << endl;
break;
}
} while ( choice != 4 );
Just put all the couts and the cin in there
#include <iostream>
using namespace std;
int main()
{
int choice = 0;
while (choice != 4)
{
cout << " Serendipity Booksellers\n";
cout << " Main Menu\n";
cout << "\n 1. Cashier Module";
cout << "\n 2. Inventory Database Module";
cout << "\n 3. Report Module";
cout << "\n 4. Exit\n";
cout << "\n Enter Your Choice: ";
cin >> choice;
}
return 0;
}
You are taking user input for the choice and care for it to repeat the menu for each time the choice is not '4'.
#include <iostream>
using namespace std;
int main()
{
int choice;
do {
cout << " Serendipity Booksellers\n";
cout << " Main Menu\n";
cout << "\n 1. Cashier Module";
cout << "\n 2. Inventory Database Module";
cout << "\n 3. Report Module";
cout << "\n 4. Exit\n";
cout << "\n Enter Your Choice: ";
cin >> choice;
}while (choice != 4);
return 0;
P.S.: it is good practice to use 'endl' to end a line rather than "\n"-- e.g.:
#include <iostream>
using namespace std;
int main()
{
int choice;
do {
cout << "Serendipity Booksellers" << endl;
cout << "Main Menu" << endl;
cout << "1. Cashier Module" << endl;
cout << "2. Inventory Database Module" << endl;
cout << "3. Report Module" << endl;
cout << "4. Exit" << endl;
cout << "Enter Your Choice: " << endl;
cin >> choice;
} while (choice != 4);
return 0;

Switch menu calculator will not display arithmetic

Sorry if the title is confusing, I did not know how to word my issue very well.
I understand some of my program is not finished as of right now, I'm currently only working on the keyboard input sections.
Basically, I have to create a program that allows the user to either input a file with set integers or have the user enter two integers of their own. This user is also asked which arithmetic they would like performed on their integers. I created a menu and sub menus using a switch statement that would allow the user to easily navigate to their destination.
My problem is, when I try to use the input through keyboard option, my program fails to display the calculated variable. I can fully navigate to the option and even input my integers but when the program displays the final answer it states: "The total is: menu" and then it kicks me back to the main menu.
My specific example:
User selects (2) for keyboard input.
User selects (1) for addition arithmetic
User enters an integer (1)
User enters another integer (2)
Program displays "The total is: Menu"
Program loops back to main menu.
Here is my code:
#include "complx.h"
#include <iostream> using namespace std;
ifstream infile ("in.dat");
int main() {
int choiceOne, choiceOneSubMenu, choiceTwoSubMenu, digitOne, digitTwo, digitTotal;
bool menu = true;
do{
cout << "Menu \n";
cout << "=========== \n";
cout << "(1) Input from a file \n";
cout << "(2) Input from the keyboard \n";
cout << "(3) Exit the program \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOne;
switch (choiceOne) {
case 1:
cout << "You chose input from a file \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOneSubMenu;
switch (choiceOneSubMenu) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
break;
case 2:
cout << "You chose input from the keyboard \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceTwoSubMenu;
switch (choiceTwoSubMenu)
{
case 1:
cout << "You chose addition \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne + digitTwo);
cout << "The total is: " + digitTotal;
break;
case 2:
cout << "You chose subtraction \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne - digitTwo);
cout << "The total is: " + digitTotal;
break;
case 3:
cout << "You chose multiplication \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne * digitTwo);
cout << "The total is: " + digitTotal;
break;
case 4:
cout << "You chose division \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne / digitTwo);
cout << "The total is: " + digitTotal;
break;
}
break;
case 3:
cout << "You have chosen to exit";
}
}while(choiceOne!=3);
}
cout << "The total is: " + digitTotal;
you can't simply convert digit to string and concatenate it using + operator.
Edit:
you can do it like this:
cout << "The total is: " << itoa(digitTotal);
or these

Switch statement error in C++

Hello guys would someone please help me to fix this code of mine? It runs but it doesn't perform the task it should do. Once you run it, it automatically jumps in to the default statement and skips the cases. Please help me fix this thanks!
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int choice;
char nbaPlayer, tele, food, subject, x;
cout << "This program determines your favorites.\n\n";
cout << "Please select the number of your corresponding choice.";
cout << "\n1. NBA Player";
cout << "\n2. Teleserye";
cout << "\n3. Food";
cout << "\n4. Subject";
cout << "\n5. Exit";
switch (choice)
{
case 1:
cout << "You have chosen NBA Player.\n";
cout << "Please enter your favorite NBA Player. \n";
cin >> nbaPlayer;
cout << "Your favorite NBA player is " << nbaPlayer;
break;
case 2:
cout << "You have chosen Teleserye.\n";
cout << "Please enter your favorite teleserye. \n";
cin >> tele;
cout << "Your favorite teleserye is " << tele;
break;
case 3:
cout << "You have chosen food.\n";
cout << "Please enter your favorite food. \n";
cin >> food;
cout << "Your favorite food is " << food;
break;
case 4:
cout << "You have chosen subject.\n";
cout << "Please enter your favorite subject. \n";
cin >> subject;
cout << "Your favorite subject is " << subject;
break;
case 5:
cout << "You chose to exit.\n";
break;
default:
cout <<"\nInvalid input";
}
getch();
}
You simply forgot:
cin>>choice
after you display available choice options. So when you never assign value to choice, it'll always choose default.