Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In the below code, I need help with the alternatives of switch case, I mean how can I avoid switch and use the other option to execute the code.
Code:
cout << " *********************Intensive Program 1***********************\n\n" << endl;
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.\n";
cout << "\nPlease enter your choice: ";
cin >> choice;
while (choice <4){
switch (choice){
case 1:
// For adding cirles to the class
cout << " Enter the value for the radius of the circle: ";
cin >> radius;
cout << " Enter the value for the center of the circle: ";
cin >> center;
myCircle[thisPosition] = new Circle(radius, center);
myCircle[thisPosition]->PrintCircle();
thisPosition++;
break;
case 2: // For printing a particular cirle from the list of cirles
cout << " Enter the Value for which Circle to Print: ";
cin >> printPosition;
myCircle[printPosition - 1]->PrintCircle();
break;
case 3: // For printing all the circles in the class object array pointer list
cout << "\n";
for (int i = 0; i < thisPosition; i++){
myCircle[i]->PrintCircle();
cout << "\n==============================" << endl;
}
break;
case 4:
cout << "\n Good Bye !!! \n " << endl;
break;
}
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.";
cout << "Please enter your choice: ";
cin >> choice;
system("pause");
}
}
You could also use a std::vector<std::function<void()>> choices(5);... and call it like choices[choice](); after you filled it with the alternative choices like choices[1] = &function_1; etc...
But I have a feeling the real question is not really about how to avoid switch...
[EDIT]
Based on your comment, I think the question is how to avoid duplicating the "menu" output. Simply refactor like this using a do..while:
cout << " *********************Intensive Program 1***********************\n\n" << endl;
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.\n";
cout << "\nPlease enter your choice: ";
do
{
cin >> choice;
switch (choice){
case 1:
// For adding cirles to the class
cout << " Enter the value for the radius of the circle: ";
cin >> radius;
cout << " Enter the value for the center of the circle: ";
cin >> center;
myCircle[thisPosition] = new Circle(radius, center);
myCircle[thisPosition]->PrintCircle();
thisPosition++;
break;
case 2: // For printing a particular cirle from the list of cirles
cout << " Enter the Value for which Circle to Print: ";
cin >> printPosition;
myCircle[printPosition - 1]->PrintCircle();
break;
case 3: // For printing all the circles in the class object array pointer list
cout << "\n";
for (int i = 0; i < thisPosition; i++){
myCircle[i]->PrintCircle();
cout << "\n==============================" << endl;
}
break;
case 4:
cout << "\n Good Bye !!! \n " << endl;
break;
}
system("pause");
}
} while(choice != 4);
If you still want the menu to be repeated for each choice, then simply cut and paste the menu printing at the start of the do..while loop.
As a side note, I strongly suggest you read https://mikeash.com/getting_answers.html for the next time.
Using if-else ladder is one of the alternatives for switch statements.
cout << " *********************Intensive Program 1***********************\n\n" << endl;
cout << "\nHere's the menu of choices: ";
cout << "\n1. Add a Circle.\n";
cout << "2. Print a Circle. \n";
cout << "3. Print all Cricles \n";
cout << "4. Exit.\n";
cout << "\nPlease enter your choice: ";
cin >> choice;
while (choice <4){
if(choice==1)
{
// For adding cirles to the class
cout << " Enter the value for the radius of the circle: ";
cin >> radius;
cout << " Enter the value for the center of the circle: ";
cin >> center;
myCircle[thisPosition] = new Circle(radius, center);
myCircle[thisPosition]->PrintCircle();
thisPosition++;
}
else if(choice==2)
{ // For printing a particular cirle from the list of cirles
cout << " Enter the Value for which Circle to Print: ";
cin >> printPosition;
myCircle[printPosition - 1]->PrintCircle();
}
else if(choice==3) // For printing all the circles in the class object array pointer list
{
cout << "\n";
for (int i = 0; i < thisPosition; i++){
myCircle[i]->PrintCircle();
cout << "\n==============================" << endl;
}
}
else
{
cout << "\n Good Bye !!! \n " << endl;
}
}
system("pause");
}
Related
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);
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;
}
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');.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having trouble with this do-while loop menu for a program I'm working on for school. I've checked, and as far as I'm concerned I have written the code correctly. However, when testing, if I type 'y' or 'n' the result is the same: the menu streaming down 100's of times non stop until I exit the program. Any idea on what I'm doing wrong and how I can get it to display the menu properly every time? Thanks in advance.
#include <iostream>
#include <iomanip>
#include <string>
#include "CashRegister.h"
#include "InventoryItem.h"
using namespace std;
int main()
{
// Variables
int selection, numUnits, cont;
double price;
// Use the first constructor for the first item
InventoryItem item1;
item1.setCost(5.0);
item1.setDescription("Adjustable Wrench");
item1.setUnits(10);
// Use the second constructor for the second item
InventoryItem item2("Screwdriver");
item2.setCost(3.0);
item2.setUnits(20);
// Use the third constructor for the remaining items
InventoryItem item3("Pliers", 7.0, 35);
InventoryItem item4("Ratchet", 10.0, 10);
InventoryItem item5("Socket Wrench", 15.0, 7);
do
{
cout << "#\t" << "Item\t\t\t" << "qty on Hand" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "1\t" << item1.getDescription() << "\t" << setw(3) << item1.getUnits() << endl;
cout << "2\t" << item2.getDescription() << "\t\t" << setw(3) << item2.getUnits() << endl;
cout << "3\t" << item3.getDescription() << "\t\t\t" << setw(3) << item3.getUnits() << endl;
cout << "4\t" << item4.getDescription() << "\t\t\t" << setw(3) << item4.getUnits() << endl;
cout << "5\t" << item5.getDescription() << "\t\t" << setw(3) << item5.getUnits() << endl;
cout << "Which item above is being purchased? ";
cin >> selection;
// Validate the selection
while (selection < 1 || selection > 5)
{
cout << "Error, please make a valid item selection: ";
cin >> selection;
}
cout << "How many units? ";
cin >> numUnits;
// Validate the quantity of units to make sure it isn't a negative value
while (numUnits < 0)
{
cout << "Error, please enter a valid quantity: ";
cin >> numUnits;
}
// Use a switch statement to figure out which cost to pull
switch (selection)
{
case 1: {price = item1.getCost();
item1.changeUnits(numUnits); }
break;
case 2: {price = item2.getCost();
item2.changeUnits(numUnits); }
break;
case 3: {price = item3.getCost();
item3.changeUnits(numUnits); }
break;
case 4: {price = item4.getCost();
item4.changeUnits(numUnits); }
break;
case 5: {price = item5.getCost();
item5.changeUnits(numUnits); }
break;
}
// Create a CashRegister object for this particular selection
CashRegister transaction(price, numUnits);
// Display the totals
cout << fixed << showpoint << setprecision(2);
cout << "Subtotal: $" << transaction.getSubtotal() << endl;
cout << "Sales Tax: $" << transaction.getSalesTax() << endl;
cout << "Total: $" << transaction.getPurchaseTotal() << endl;
// Find out if the user wants to purchase another item
cout << "Do you want to purchase another item? Enter y/n: ";
cin >> cont;
} while (cont != 'n' && cont != 'N');
system("pause");
return 0;
}
Your loop will never break unless you explicitly enter 110 which is the 'n' char in ASCII Codes or 78 which is the 'N'. So change your cont declaration from int cont; to char cont; and then you won't get the infinite loop anymore, and its condition will be valid to possibly break by then unless you have another hidden logical error which will require you to debug it.
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