holeMenuProgram.cpp:38:1 Error: Expected Primary-Expression before '}' token - c++

I am creating a menu using switch cases but when I got to filter out unavailable choices using a do-while statement I am getting an odd error. Could I get some insight on what I am doing wrong here? Thank you in advance as I am a new programmer. I am aware the case switches have nothing in them but this shouldn't stop it from compiling correctly.
//Programmer: Lane Floyd Date: 2/17/2020
//File: holeMenuProgram.cpp
//Description:
#include <iostream>
using namespace std;
int main()
{
int menuChoice; //User input 1-4 for menu choice.
do
{
// Below is the menu.
cout << " Menu " << endl;
cout << " ---- " << endl;
cout << "1. Enter a number " << endl;
cout << "2. Power the number " << endl;
cout << "3. Cube root of the number " <<endl;
cout << "4. Quit" <<endl;
cout << "Input Choice: \r";
cin >> menuChoice;
} while (menuChoice != 1 || menuChoice != 0);
switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
}
return 0;
}

A label must precede a statement. You could write for example
switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
;
}
placing a null statement before the closing brace in the switch statement.
Or to make the code more readable you could insert at least one break statement like
switch(menuChoice)
{
case 1:
case 2:
case 3:
case 4:
break;
}

Related

I can't print a vector inside a switch case

This is my first question here. I am just beginning to learn C++ and I am stuck at this exercise:
Your program should display a menu options to the user as follows:
P--Print the vector
A--Add a number
M--Display mean of the number
S--Display the smallest number
L--Display the largest number
Q--Quit
Enter your choice:
Basically, I need to do a menu for these operations. I got stuck at the part of printing the vector. I already tried to use (for auto:....) and also tried with the normal index (int i = 0...), but the contents in the vector don't appear, only the message "This is your list of numbers:". I also tried to create a function to make sure that the user was inputting an integer in the A case, but did not know how to do it :(
This is my code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
bool control_end = false;
do
{
cout << "Please enter your desired function within the menu: " << endl;
cout << endl;
cout << "P--Print list of numbers" << endl;
cout << "A--Add a number"<< endl;
cout << "M--Display mean of the number"<< endl;
cout << "S--Display the smallest number"<< endl;
cout << "L--Display the largest number"<< endl;
cout << "Q--Quit "<< endl;
cout << endl;
cout << "Enter your choice: ";
char letra{};
cin >> letra;
vector<int> vector{};
switch(letra)
{
case 'A':
case 'a':
{
cout << "Please enter the value (integer) to be added to vector: ";
int value_add;
cin >> value_add;
cout << endl;
vector.push_back(value_add);
//(later do it) create a function to ensure that the value is an integer
break;
}
case 'p':
case 'P':
{
cout << "This is your list of numbers: \n";
for (unsigned int i=0 ;i < vector.size(); i++)
cout << vector[i] << " ";
break;
}
case 'm':
case 'M':
cout << "test M";
break;
case 's':
case 'S':
cout << "test S";
break;
case 'L':
case 'l':
cout << "test L";
break;
case 'q':
case 'Q':
cout << "Thank you for using the program" << endl;
control_end = true;
break;
default:
cout << "Invalid char. " << endl;
cout << endl;
break;
}
for(auto vec: vector)
cout << vec << endl;
}
while (control_end !=true);
return 0;
}
You created the variable vector inside your loop, so at the end of the loop the variable get destroyed and a new empty one is created on the next iteration. So, if you want your variable to retain values between loop iterations, you should declare it outside of the loop scope, the same way you did with the variable control_end.

C++ The loop in my switch format isn't allowing me to display menu options or accept input?

I'm needing to write a loan program for a course and I have this bit of code thus far:
/*
Project: Course Project - Loan Calculator
Program Description: To calculate customer's loan
information including interest rate, monthly
payment and number of months payments will be
required for.
Developer: ___________
Last Update Date: June 1st, 2020
*/
#include <iostream>
#include <string>
//variables listed here
double loanAmt = 0, intRate = 0, loanLength = 0;
std::string CustName[0];
int choice; //menu choice identifier
using namespace std;
int main()
{
//Welcome User
cout << "Thank you for using our calculator." << endl;
cout << endl;
cout << "Please enter the number of your preferred calculator and press 'Enter': " << endl;
cout << endl;
cin >> choice;
{
while (choice !=4);
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
}
}
I've tried moving the cin << choice; to many different spots, including inside of the while loop written for the switch, but I can only seem to get it to show the prompt for the menu, but not the menu options themselves and it isn't taking input. If it makes a difference, I'm using Xcode for the first time and I need it to run there as I can't run C++ on my version of Visual studio (on Mac). Where may I have gone wrong here and any insight into why my version is wrong is appreciated as I'm still very new to coding overall. Thank you.
You had this:
while (choice !=4);
This makes it so that your code never continues further than this while loop. Unless your compiler removes this line during optimization.
This is the correct way:
while (choice !=4) {
switch (choice)
{
case 1:
cout << "Monthly Payment Calculator:";
cout << endl;
break;
case 2:
cout << "Interest Rate Calculator:";
cout << endl;
break;
case 3:
cout << "Payment Term (In Months):";
cout << endl;
case 4:
cout << "Exit Calculator:";
default: //all other choices
cout << "Please Select A Valid Option.";
break;
}
cin >> choice; // have it right after the switch
}

Exiting the Game from the Menu is not working

So I have a Menu that I used to run my text based game. The problem is that I can't seem to exit my game.Every time I run it, I can do option 1 and go to my game, and options 2 and 3 work just fine. But For option 4, I am unable to exit my game. All it does is print out what I ask it to print out, before giving the menu options again (as if it was just looping).
I have googled a lot, and tried to figure out why but I am not sure.
If someone can advise me what to do or tell me where my mistake is, it would be greatly appreciated. Please let me know if you want to see more code. All I have displayed here is the Menu Function.
void menu() {
char choice = -1;
while(choice != '1')
{
cout << "\n* * * * *" << endl;
cout << " The Dark Maze\n";
cout << "\n* * * * *" << endl;
cout << "\n=====================";
cout << "\n Main Menu |";
cout << "\n=====================";
cout << "\n 1 - Start Game |";
cout << "\n 2 - Instructions |";
cout << "\n 3 - Storyline |";
cout << "\n 4 - Exit |";
cout << "\n=====================";
cout << "\n";
cout << "\n Enter choice: ";
cout << "\n";
cin >> choice;
switch (choice)
{
case '1':
cout << "\n" << endl;
cout << "\n But we can't start the game just yet..." << endl;
break; //heads to game
case '2':
Instructions();
break;
case '3':
Storyline();
break;
case '4':
cout << "\n Well, if you really don't want to play... you don't have to." << endl;
break; //just say exit?? break isnt making it stop
default:
cout << "Invalid Character entered\n";
cout << "\n";
cout << "Press Space to continue\n";
}// end of switches
cin.get();
} // end of while
}// end of menu
You shouldn't be using while loop for this. Try do-while loop for this kind of menus like this:
do
{
// your menu here...
cin >> choice;
switch ( choice )
{
case ...
...
}
// cin.get();
// ^^^^^^^^^^ You don't need this...
} while ( choice != '4' );
Some points to help you:
Use an enum to define your menu choices. (OR an enum class).
You can simply write a printMenu() function to print the menu in the main loop. Another function to process the choice.
For example:
void startGame()
{
char choice = INVALID_OPTION; // INVALID_OPTION => default invalid value
do
{
printMenu();
cin >> choice;
processChoice( choice );
} while ( choice != EXIT ); // EXIT => #define or an enum
}
You can use exit() to terminate your program with a given return value:
case '4':
std::cout << "blahblahblah";
std::exit(0);
break; // No longer necessary
It's prerequisite and prototype is
#include <cstdlib>
namespace std{
void exit(int status);
}
just use return instead break blow case '4' . not perfect but can work.

Switch statement for Coke Machine code

I have always used if else statements over switch statements, but I decided I wanted to try a switch out. I did the basic Coke Machine program with a switch and I cannot for the life of me figure out why it does not work how it should. When I use a number 1-5 for my input it continues to give the the switch default error message instead of the case cout statements (such as "You chose Coke"). Obviously something must be wrong that I am not seeing/
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Beverage List" << endl;
cout << "Coke = 1" << endl;
cout << "Dr. Pepper = 2" << endl;
cout << "Water = 3" << endl;
cout << "Sprite = 4" << endl;
cout << "Lemonade = 5" << endl << endl << endl;
cout << "Enter a number to choose a beverage: ";
cin >> number;
switch (number)
{
case '1':
cout << "You chose Coke";
break;
case '2':
cout << "You chose Dr. Pepper";
break;
case '3':
cout << "You chose Water";
break;
case '4':
cout << "You chose Sprite";
break;
case '5':
cout << "You chose Lemonade";
break;
default:
cout << "Error: Choice was not valid. Here is your money back.";
}
cout << "\n";
system("pause");
return 0;
}
The character '1' is not the same as the number 1.
Change
case '1':
to
case 1:
It looks like your case statements are comparing chars, not integers:
case '5':
Try this instead:
case 5:

How do I get a switch statement to loop back into a menu

Hey everyone I'm currently having some trouble in making the switch statement coming back out around to the start of the menu. Instead it proceeds into another main function that I don't want it to unless the user chooses the right selection.
here's the code I have anyway:
int main()
{
int choice;
bool menu = true;
cout <<"Please select one of the following options: \n";
cout << "1: Play\n"
"2: Help\n"
"3: Config\n"
"4: Quit\n";
cout << "Enter your selection (1, 2,3 or 4): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
if(menu)
{
switch (choice)
{
case 1:
cout << "You have chosen play";
break;
case 2:
cout << "You have chosen help\n";
cout << "Here is a description of the game Hangman and how it is played:\nThe word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
break;
case 3:
cout << "You have chosen config";
break;
case 4:
cout << "You have chosen Quit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 4!\n";
}
}
getchar();
getchar();
cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;
}
You could replace your if with a while loop
cout << "Enter your selection (1, 2, 3 or 4): ";
while (menu)
{
cin >> choice;
menu = false;
switch (choice)
{
case 1:
cout << "You have chosen play";
break;
....
default:
cout<< "Your selection must be between 1 and 4!\n";
menu = true; // incorrect input, run loop again
}
you can use a do while loop like this:
int main()
{
int choice;
bool menu = true;
do{
cout <<"Please select one of the following options: \n";
cout << "1: Play\n"
"2: Help\n"
"3: Config\n"
"4: Quit\n";
cout << "Enter your selection (1, 2,3 or 4): ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
switch (choice)
{
case 1:
cout << "You have chosen play";
int missed = playgame('programming');
break;
case 2:
cout << "You have chosen help\n";
cout << "Here is a description of the game Hangman and how it is played:\nThe word to guess is represented by a row of dashes, giving the number of letters, numbers and category. If the guessing player suggests a letter or number which occurs in the word, the other player writes it in all its correct positions";
break;
case 3:
cout << "You have chosen config";
break;
case 4:
cout << "You have chosen Quit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 4!\n";
}
}while(choice!=4);
getchar();
getchar();
cout << "You missed " << playGame("programming");
cout << " times to guess the word programming." << endl;
}
I have added the do loop before you display your menu so that everytime the user chooses a choice, the user is displayed the menu again unless the user enters 4, in which case , the program comes out of the do while loop.
You can call the function playgame() in case 1..I have edited my answer to include playgame();