C++ Code Help (While Loop) - c++

#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;

Related

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;
}

how to end the repetition of showing the menu in c++?

So, I am new to C++. I have this code where I need to input what I would like to view. What happened in my code is that, when I put an input in choice, for example 1, it will show the Peripherals list. And when I input 5 in the Peripheral List, it would go back to the Main Menu. In the Main Menu, if I inputted 4, the Main Menu is looping or repeating again and again. I need to exit it or end if I input 4.
#include <iostream>
using namespace std;
int main()
{
int choice;
int pick;
char view;
cout << "\n" << endl;
cout << "\t \t -------------------------------------------------------------------" << endl;
cout << "\t \t \t \t \t WELCOME TO SHOPPING SPREE" << endl;
cout << "\t \t -------------------------------------------------------------------" << endl;
//MENU
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "\n";
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO" << endl;
cout << "[2] SteelSeries APEX PRO" << endl;
cout << "[3] Razer Kraken X" << endl;
cout << "[4] AORUS K7" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
else if (choice == 2) {
cout << "\n";
cout << "What mobile phones would you like to purchase?" << endl;
cout << "[1] Xiaomi Mi Mix 3" << endl;
cout << "[2] Oppo Reno" << endl;
cout << "[3] Realme 5" << endl;
cout << "[4] Samsung Galaxy 10" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
else if (choice == 3) {
cout << "\n";
cout << "What consoles would you like to purchase?" << endl;
cout << "[1] PlayStation 5" << endl;
cout << "[2] Nintendo Switch" << endl;
cout << "[3] PlayStation 4" << endl;
cout << "[4] XBOX S" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
}
while (pick == 5);
}
You need to do a thing when choice is 4 (currently you're not doing anything).
That thing is break: end the loop.
else if (choice == 4) {
break;
}
Alternatively, build it into the loop condition:
while (choice != 4 && pick == 5);
…though, personally, I think this is harder to follow.
The break in C or C++ is a loop control statement which is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop.
So, you simply can do:
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 4) {
break;
}

most of the code doesn't show up only the welcome message and "enter pin"?

Assignment:
When you enter an incorrect number of digits( 3 or 5 digits pin number) a
message should display “You have entered the incorrect pin number!!...you
must enter a four digits pin number.”
By showing the message that was display previously the program should
allow you to re-enter the correct number of digits.
The number of attempts should be three times.
When you have entered the correct number of digits a message should display
“Your pin has been accepted!!”
Each time you enter any amount of pin number it must show in asterisk.
Code:
#include <conio.h>
#include <iostream>
//#include <cstdlib.h>
#include <cmath>
using namespace std;
int main() {
string pass = "";
int attempts = 3;
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "< Welcome to The Bank >" << endl;
cout << "< >" << endl;
cout << "< Please Enter Pin Below >" << endl;
cout << " ^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
// attempts= getch();
while (attempts <= 3) {
cout << "*";
getch();
attempts++; // take this out and it display to infinity
// }
if (pass == "1718") {
cout << " lOGIN IN..." << endl << endl;
attempts = -1;
}
else {
cout << "\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
if (attempts == 0) {
cout << "Exceed the Pin attempts. Try Later. " << endl << endl;
}
if (attempts == -1) {
cout << "********************************" << endl;
cout << "* Welcome to Magali's Bank *\n";
cout << "* Select Option: *\n";
cout << "* 1. Check Balance *\n";
cout << "* 2. Withdraw *\n";
cout << "* 3. Deposit *\n";
cout << "* 4. Exit *\n";
cout << "********************************\n";
int balance = 500;
float withdraw;
float deposit;
int user;
cout << "Enter Number: ";
cin >> user;
while (user != 4) {
switch (user) {
case 1:
cout << " Your balance is: " << balance << endl;
break;
case 2:
cout << "Enter the amount you want withdraw: ";
cin >> withdraw;
balance = balance - withdraw;
break;
case 3:
cout << "Enter the amount you want to deposit: ";
cin >> deposit;
balance = balance + deposit;
break;
default:
cout << " Need to type 1 for Balance, 2 to Withdraw, 3 to Deposit "
"and 4 to Exit. ";
}
cout << "Enter Number: ";
cin >> user;
}
cout << "\nTHANKS FOR USING THE SYSTEM!\n";
}
}
return 0;
}
Your code has lots of inefficiencies but as you requested. I'm only debugging only a part of it where you need to take input and display error.
string pass="";
int attempts=3;
const string pin = "1718";
//DO whatever you want
//while loop to take input
while (attempts >0)
{
cout << "\nEnter Pin Number: " << endl;
cin >> pass;
for(auto i : pass)
cout<<"*";
if(pass == pin){
attempts = 3;
cout<<"Your Pin has been accepted.\n";
break;}
else{
cout <<"\nWRONT PIN-TRY AGAIN: " << endl << endl;
attempts--;
cout << " REMAINING ATTEMPTS: " << attempts << endl << endl;
}
}
if (attempts == 0)
{
cout << "Exceed the Pin attempts. Try Later. "<< endl << endl;
cout<<"Your account has been locked for a day .\n";
exit(0);
}
Comment out if you didn't understand any part.

User answer choices displaying the number choice and not what the number is? C++

I am writing a car dealer program where you ask the user to enter their choice (1. Ford 2.Mustang) But instead of displaying the choices they have chosen in the end, it displays the number choice. Instead of displaying " You have chosen a red 2015 Chevrolet Malibu " or something like that, it displays " You have chosen a 1 1 1 1." Here is the code.
UPDATE I have switched to using cases and I still get the same output.
#include <iostream>
using namespace std;
void cars();
int main()
{
int option;
cout << "Hello! Thank you for using CarRight Solutions!\n";
cout << "We have a nice selection of sudans to choose from. Press 1 to start
your search for the right car. \n\n" << endl;
cout << "1. Cars\n";
cin >> option;
if (option == 1)
{
cars();
}
system("pause");
return 0;
}
void cars ()
{
int make, model, year, color;
cout << "\n *****CARS SELECTION***** \n";
cout << "Select a make. \n" << endl;
cout << "1. Chevorlet \n";
cout << "2. Nissan \n";
cout << "3. Honda \n";
cout << "4. Toyota \n";
cin >> make;
switch (make) {
case 1:
cout << "\n You have selected Chevorlet.\n";
cout << "Please select a model. \n";
cout << "1. Malibu \n";
cout << "2. Impala \n";
cin >> model;
switch(model) {
case 1:
cout << "\n You have selected Malibu. \n";
cout << "Please select a year. \n";
cout << "1. 2017 \n";
cout << "2. 2018 \n";
cout << "3. 2019 \n";
cin >> year;
switch(year) {
case 1:
cout << "\n You have selected 2017. \n";
cout << "Select a color. \n\n";
cout << "1. Black \n";
cout << "2. White \n";
cout << "6. Blue \n";
cin >> color;
switch(color) {
case 1:
cout << "\n You have selected Black.\n";
cout << "A" << color << "," << year << "," << make << "," << model
<< " will be around $20,000.";
}
}
}}}
The output is still:
//A1,1,1,1 will be around $20,000.
You printing "make" value
cout << "\n You have selected" << make
But you need to use switch operator like
switch(make){
case 1:
break;
case 2:
break;
}

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

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');.