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
Related
I have an assignment for my intro C++ class to make a program that has a user select C, D, or E to process a check, deposit a check, or end the program respectively. The professor specified that if a user chooses C or D, he would like to accept both the selection and the amount of money in the same line. For example, if a user wants to deposit a check for $20, they would enter "D 20" in one line. I have it set up as such:
cout << "Enter the beginning balance:\n\n";
cin >> balance;
cout << "Commands:\n";
cout << "C - process a check\n";
cout << "D - process a deposit\n";
cout << "E - end the program\n\n";
while (choice != 'E')
{
cout << "Enter a transaction:\n";
cin >> choice >> amount;
Followed by a switch statement. The code itself works properly when entering C or D, but when I go to enter E to end the program, it will only work if I add a number to the end, because the line asking for input wants a char and a float. However, in the example output my professor showed, you could just enter E and it would terminate. Is there any way around this? How can I set it up so it accepts E differently from C and D?
EDIT: Here is the switch statement:
switch(choice)
{
case 'C':cout << fixed << setprecision(2);
cout << "Processing check for $" << amount << endl;
balance = processCheck(amount, balance);
cout << "Balance: $" << balance << endl;
break;
case 'D':cout << fixed << setprecision(2);
cout << "Processing deposit for $" << amount << endl;
balance = depositCheck(amount, balance);
cout << "Balance: $" << balance << endl;
break;
case 'E':cout << "Processing end of month\n";
cout << "Final balance: $" << balance << endl;
break;
default : cout << "Invalid choice\n";
}
Already answered in comments, but anyway:
Replace this code
cin >> choice >> amount;
by gradual and conditional input code:
cin >> choice;
if (choice == 'C' || choice == 'D')
cin >> amount;
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 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");
}
I created a text-based game to help improve my C++ skills since I am very new to it. I'm trying to learn the basics n' all.
Here is the program. In the switch statement, if a person were to select "3" it would broadcast an Error saying you can only select "1" or "2".
** The issue is the program continues and doesn't make the person RECHOOSE the selection. It goes right to Difficulty selecting.
What method do I use to force the program to halt until player chooses valid selection?
Thanks!
#include <iostream>
using namespace std;
int main()
{
cout << "\tWelcome to my text based game!\n";
char userName[100];
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
int pickRace;
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
}
int difficulty;
cout << "\nPick your level difficulty: \n";
cout << "1 - Easy\n";
cout << "2 - Medium\n";
cout << "3 - Hard\n";
cout << "Pick your level difficulty: ";
cin >> difficulty;
switch (difficulty)
{
case 1:
cout << "You picked Easy" << endl;
break;
case 2:
cout << "You picked Medium" << endl;
break;
case 3:
cout << "You picked Hard" << endl;
break;
default:
cout << "Error - Invalid input, only 1,2 or 3 are allowed" << endl;
}
return 0;
}
You need to use loops. Wrap the input and the switch after it in a loop and break out of it when the input is valid.
Use a do ... while loop, like this
int pickRace;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Pick your race: ";
cin >> pickRace;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
break;
}
}
while (pickRace != 1 && pickRace !=2);
This will keep on looping while the condition at the bottom is true (i.e. while they haven't picked a valid option).
One other comment. Since you are new you should really get into the habit of using string not char arrays. Char arrays will get you into all sorts of trouble in the future, so start using strings now. There's no point in learning bad habits.
#include <string>
string userName;
cout << "\nPlease enter your username: ";
cin >> userName;
cout << "Hello, " << userName << "!\n\n";
You can do - while loop with flags. The flag is by default false therefore allowing only one time input. If there is a need to enter another time, determined and controlled by the default case, then the flag is set to true, which makes the loop iterate once more. At the beginning of each iteration flag is reset to false, so each time it is assumed that this is the last one. Using flag will make the breaking operation much simple, and avoid complex while conditions.
int flag = 0;
do
{
cout << "Please pick your race: \n";
cout << "1 - Human\n";
cout << "2 - Orc\n";
cout << "Enter Choice: ";
cin >> pickRace;
flag = 0;
switch (pickRace)
{
case 1:
cout << "You picked the Human race." << endl;
break;
case 2:
cout << "You picked the Orc race." << endl;
break;
default:
cout << "Error - Invalid input; only 1 or 2 allowed!" << endl;
flag = 1;
}
} while (flag);