I need help on how to allowing player to input a number in this code:
using namespace std;
int main()
{
char name[50];
menu:
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
cin.getline(number, 1);
if(number = 1) {
start_game();
}
else if(number = 2){
help();
}
else if(number = 3){
exit();
}
else {
goto menu;
}
return 0;
}
It says that there is a problem in the cin.getline(number,1) in my program. If anyone knows how to fix it, please tell me. Thanks
Do it like this:
using namespace std;
int main()
{
char name[50];
while (true) // Use a loop instead of goto since it is more readable for other developers
{
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
int number; // You didn't declare `number`
cin >> number; // You used getline wrong
if (number == 1) { // Use `==` for comparing
start_game();
break;
}
if (number == 2) {
help();
break;
}
if (number == 3) {
exit();
break;
}
}
return 0;
}
I explained some fixes in the comments.
#include <iostream>
using namespace std;
int main()
{
char name[50];
int number ;
menu:
cout << "Select your choice..." << endl;
cout << "1) Start Game" << endl;
cout << "2) Help" << endl;
cout << "3) Exit Game" << endl;
cin >> number;
if(number == 1) {
start_game();
}
else if(number == 2){
help();
}
else if(number == 3){
exit();
}
else {
goto menu;
}
return 0;
}
Related
I have written a code that displays a rock-paper-scissors game against the computer. I would like to add a feature where I can create a text file in order to store the person's score and the computer score and keep track of the score but I don't know how to do it. Thank you in advance!
Here is my code.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void rock_paper_scissors()
{
static int userscore = 0;
static int computerscore = 0;
string playername;
int userchoice;
int computerchoice;
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
cout << "Please enter your choice\n";
cin >> userchoice;
cout << endl;
while (!(userchoice > 0 && userchoice <= 3))
{
cout << "invalid choice. please enter a number between 1 and 3\n";
cin >> userchoice;
}
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
}
else
{
cout << "computer wins!" << endl;
}
cout << "thank you for playing!\n";
string restart;
cout << "Would you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
if (restart == "y")
{
rock_paper_scissors();
}
}
int main()
{
cout << "MAIN\n";
rock_paper_scissors();
return 0;
}
Why do you need to write the data into in file? Updating a simple txt file after each round could be cumbersome, as you always result in a single score at the end of the program. I would suggest changing the type of the rock_paper_scissors into int indicating the score achieved by the player in a single lot. The intermediate results are irrelevant. Just place the game loop into your main function and do not use a recursive function call and static function variables here. Otherwise, the player is forced to enter his name for every single lot.
Moreover, I tested your code and you have to change your error handling. I typed in "rock" and the program stuck in an infinity loop "invalid choice. please enter a number between 1 and 3\n" Whereby it is not possible to make another entry. As I entered a string instead of an integer, you have to reset the console. Beware of the dumbest possible user.
Moreover, you should seed your program to avoid identical computer choices in each game. This could be done with srand(time(NULL)).
Eventually, I write the tracked score into a score file at the end of the main function using the fstream standard library.
#include <iostream>
#include <string>
#include <algorithm> // min max
#include <fstream> // read/write from/to files
#include <time.h> // time
using namespace std;
int rock_paper_scissors(const std::string& playername);
int main()
{
srand(time(NULL));
cout << "MAIN\n";
cout << "Hello to rock-paper-scissors!\n";
cout << "rock beats scissors, scissors beats paper and paper beats rock." << endl;
cout << "choose 1 for rock, 2 for paper, 3 for scissors.\n";
int userscore = 0;
int computerscore = 0;
std::string playername;
std::string restart;
cout << "please enter your name." << endl;
cin >> playername;
cout << endl;
do
{
int result = rock_paper_scissors(playername);
cout << "thank you for playing!\n";
userscore += result;
computerscore += std::max(0, 3 - 2 * result);
cout << playername << "'s score: " << userscore;
cout << "\ncomputer's score: " << computerscore;
cout << "\nWould you like to play again?" << endl << "(y)es / (n)o" << endl;
cin >> restart;
} while (restart == "y");
std::ofstream ofile;
ofile.open("scorefile.txt");
ofile << "Scores:\n" << playername << ": " << userscore;
ofile << "\nComputer: " << computerscore;
ofile.close();
return 0;
}
int rock_paper_scissors(const std::string& playername)
{
int userchoice;
int computerchoice;
cout << endl << endl;
do {
cout << "Please enter your choice\n";
if (std::cin >> userchoice)
{
if (userchoice > 0 && userchoice <= 3)
{
break;
}
else
{
cout << "invalid choice. please enter a number between 1 and 3\n";
continue;
}
}
else if (!cin.bad() && !cin.eof())
{
// a non integer value entered
cerr << "invalid choice. please enter a number between 1 and 3\n";
// Reset error state
cin.clear();
// remove error input
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
}
} while (true);
if (userchoice == 1)
{
cout << playername << " picked rock." << endl;
}
else if (userchoice == 2)
{
cout << playername << " picked paper." << endl;
}
else if (userchoice == 3)
{
cout << playername << " picked scissors." << endl;
}
computerchoice = (rand() % 3) + 1;
if (userchoice == 1 && computerchoice == 3)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 2 && computerchoice == 1)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == 3 && computerchoice == 2)
{
cout << playername << " wins!" << endl;
return 3;
}
else if (userchoice == computerchoice)
{
cout << " draw!" << endl;
return 1;
}
else
{
cout << "computer wins!" << endl;
return 0;
}
}
I'm coding a program for rock paper scissors where there is a user player and a computer player. I believe everything is fine up to the bool value returning function. It needs to take two arguments (computer's choice, player's choice) and see if they are equal to print out "Tie". However, I'm getting an error that says undeclared identifiers for my two arguments.
I tried changing it to an int function instead of bool. and have my bool statements in main, but it did not work
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int getComputerChoice();
int getPlayerChoice();
bool isTie (int, int);
int main()
{
char choice;
int compChoice;
int plaChoice;
do
{
cout << "ROCK PAPER SCISSORS MENU" << endl;
cout << "--------------------------" << endl;
cout << "p)Play Game" << endl;
cout << "q)Quit" << endl;
cout << "Please enter your choice : " << endl;
cin >> choice;
while (choice != 'p' && choice != 'q')//or if//why &&
{
cout << "Invalid selection. Try again." << endl << endl << endl;
cin >> choice;
}
switch (choice)
{
case 'p':
compChoice = getComputerChoice();
plaChoice = getPlayerChoice();
if (plaChoice == 1)
{
cout << "You chose: Rock" << endl;
}
else if (plaChoice == 2)
{
cout << "You chose: Paper" << endl;
}
else
{
cout << "You chose: Scissors" << endl;
}
if (compChoice == 1)
{
cout << "The computer chose: Rock" << endl;
}
else if (compChoice == 2)
{
cout << "The computer chose: Paper" << endl;
}
else
{
cout << "The computer chose: Scissors" << endl;
}
if (isTie(compChoice, plaChoice))
{
cout << "It is a Tie!";
}
break;
case 'q':
cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
break;
}
} while (choice != 'q');
return 0;
}
int getComputerChoice()
{
int comp = 0;
int rando = 0;
srand((unsigned int)time(NULL));
rando = rand() % 3 + 1;
switch (rando)
{
case 1:
comp = 1;
break;
case 2:
comp = 2;
break;
case 3:
comp= 3;
break;
return comp;
}
}
int getPlayerChoice()
{
int player;
cout << "Rock, Paper or Scissors?" << endl;
cout << "1) Rock" << endl;
cout << "2) Paper" << endl;
cout << "3) Scissors" << endl;
cout << "Please enter your choice: " << endl;
cin >> player;
while (player != 1 && player != 2 && player != 3)
{
cout << "Invalid" << endl;
cin >> player;
}
return player;
}
bool isTie(compu, playa)
{
if (compu == playa)
return true;
else
return false;
}
These are the error messages I'm getting
compu': undeclared identifier
playa': undeclared identifier
'isTie': redefinition; previous definition was 'function'
see declaration of 'isTie'
'isTie': function-style initializer appears to be a function definition
isTie is a function that has 2 parameters.
From your code, I can see that it's expecting 2 integers.
So you need to update the function signature to:
bool isTie(int compu, int playa)
This is my first simple program. It keeps printing out Guess what it is. non-stop and doesn't even ask for user input. (the next line of code.)
What is my mistake?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
return 0;
}
You need to use brackets
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
{
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
return 0;
}
By default if you don't use them only the next line will be considered part of the while loop
in your case:
while (conti)
cout << "Guess what it is. \n";
you need braces for the while loop, or it will execute just that single statement forever.
while (conti)
cout << "Guess what it is. \n";
is equivalent to:
while (conti)
{
cout << "Guess what it is. \n";
}
i.e. the loop ends there. What you need is provide the opening and closing braces for the loop at the right place.
while (conti)
{
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
You have missed the braces for while loop. You may try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti){
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
}
return 0;
}
You're missing two curly braces to widen the scope of your while-loop. Note that without curly braces the scope of any loop in C++ will stop at the first semicolon. Here's a working solution:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string userName;
cout << "Hello there.\n";
cout << "My name is TARS. \n";
cout << "What is your name? \n";
getline(std::cin, userName);
cout << userName << ", let's play a game.\n";
int secretNum;
secretNum = rand() % 20 + 1;
cout << "I'm thinking of a number between 1-20.\n";
int Guess;
bool conti = true;
while (conti)
{ // <-- This curly brace was missing
cout << "Guess what it is. \n";
cin >> Guess;
if (Guess == secretNum)
{
cout << "You read my mind!";
conti = false;
}
if (Guess < secretNum)
{
cout << "That is too low.";
}
if (Guess > secretNum)
{
cout << "That is too high.";
}
} // <-- This curly brace was also missing
return 0;
}
After finding a text-based rock paper scissors app I wrote, I decided to improve the code. Now, I want to write in for the console to clear the screen before returning to the main menu, and before executing a requested app operation. Here is the code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <algorithm>
using namespace std;
void scoreCounter(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses)
{
cout << "PLAYER SCORE:\n\n";
cout << "Wins: " << playerWins << "\n";
cout << "Losses: " << playerLosses << "\n";
cout << "Ties: " << playerTies << "\n\n\n";
cout << "COMPUTER SCORE:\n\n";
cout << "Wins: " << compWins << "\n";
cout << "Losses: " << compLosses << "\n";
cout << "Ties: " << compTies << "\n\n\n";
}
void mainMenu()
{
cout << "Main Menu\n\n";
cout << "1) Play Rock Paper Scissors\n";
cout << "2) Display scoreboard\n";
cout << "3) Exit app\n";
cout << "\n";
}
void rockPaperScissorsShoot(int playerWins, int playerTies, int playerLosses, int compWins, int compTies, int compLosses) {
cout << "Rock Paper Scissors Shoot!\n";
string playerChoice;
cin >> playerChoice;
transform(playerChoice.begin(), playerChoice.end(), playerChoice.begin(), ::tolower);
int n1;
int n2;
int n3;
srand(time(NULL));
if (playerChoice == "Rock" || playerChoice == "rock") {
n1 = rand() %3;
if (n1 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n1 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n1 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else if (playerChoice == "Paper" || playerChoice == "paper") {
n2 = rand() %3;
if (n2 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n2 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n2 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else if (playerChoice == "Scissors" || playerChoice == "scissors") {
n3 = rand() %3;
if (n3 == 0) {
cout << "You win!\n";
playerWins++;
compLosses++;
}
else if (n3 == 1) {
cout << "You lose!\n";
playerLosses++;
compWins++;
}
else if (n3 == 2) {
cout << "You tied!\n";
playerTies++;
compTies++;
}
}
else {
cout << "You made an invalid choice. Ending game...\n";
}
}
int main()
{
bool gameOver = 0;
bool exitProg = 0;
int playerWins = 0;
int playerTies = 0;
int playerLosses = 0;
int compWins = 0;
int compTies = 0;
int compLosses = 0;
int menuChoice;
while (exitProg != 1)
{
mainMenu();
cin >> menuChoice;
switch (menuChoice)
{
case 1:
{
while (gameOver == 0) {
rockPaperScissorsShoot(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
cout << "Would you like to play again? Press Y or N:\n";
char yOrN;
cin >> yOrN;
switch (yOrN) {
case 'Y': {
cout << "Ok then!\n";
break;
}
case 'y': {
cout << "Ok then!\n";
break;
}
case 'N': {
cout << "Game over.\n";
gameOver = 1;
break;
}
case 'n': {
cout << "Game over.\n";
gameOver = 1;
break;
}
}
}
cout << "\n";
break;
}
case 2:
{
scoreCounter(playerWins, playerTies, playerLosses, compWins, compTies, compLosses);
break;
}
case 3:
{
exitProg = 1;
system("PAUSE");
break;
}
}
}
return 0;
}
I considered using cin.clear(), but I don't think that will help me if I want to open a different function. What can I do?
For anyone who finds this question, I use system("CLS"); to clear the console before loading a different operation.
The instructor want us to write a program that can re displays the menu only when the user wants to restart a selection and add an option to continue with another selection.
The problem I have is when the user select a number from 1 to 4 and complete the selection, the program will ask the user if the user want to continue with another selection and when the user says no, the program still ask to select a number without ending program.
here is my code that I've written so far:
#include<iostream>
using namespace std;
int sp;
int speed = 0;
int M, K, c, x;
const int MINspeed = 10;
const int MAXspeed = 40;
int GetSpeed();
int GetMinSpeed();
int GetMaxSpeed();
int CheckContinue();
int selection;
int GetSpeed()
{
char c;
while(true)
{
cout << "\nDo you want the speed in mph or km/h? \n"
<< "\nEnter M or K followed by Enter: " << endl;
cin >> c;
if( (c != 'M')&& (c != 'K'))
{
cout << "Incorrect Selection. Try Again! \n\n";
break;
}
if ( c == 'M')
{
cout << "\nSpeed in mph: " << speed << endl;
return speed;
}
else if(c == 'K')
{
double toKmPerHour = 1.61;
double speedInKmPerHour = speed * toKmPerHour;
cout << "\nSpeed in km/h:" << speedInKmPerHour << endl;
break;
}
CheckContinue();
}
return 0;
}
int GetMinSpeed()
{
cout << "MIN speed = " << MINspeed << endl;
CheckContinue();
return 0;
}
int GetMaxSpeed()
{
cout << "MAX speed = " << MAXspeed << endl;
CheckContinue();
return 0;
}
/*int SetSpeed(int sp)
{
cout << "The Set Speed is " << sp << endl;
return 0;
}
*/
void SetSpeed()
{
cout << "Input your speed: ";
cin >> speed;
CheckContinue();
}
int CheckContinue(void)
{
char x;
while(true)
{
cout << "\nDo you want to continue with another selection? \n"
<< "\nEnter Y or N followed by Enter: " << endl;
cin >> x;
if ( x == 'Y')
{
int selection;
cout << "Selection Menu" << endl;
cout << "--------------" << endl;
cout << "\n1. Set Speed" << endl;
cout << "2. Get Speed" << endl;
cout << "3. Get MAX Speed" << endl;
cout << "4. Get MIN Speed" << endl;
cout << "5. Exit" << endl;
cout << "\nYour selection :" <<endl;
cin >> selection;
switch(selection)
{
case 1:
SetSpeed();
break;
case 2:
GetSpeed();
break;
case 3:
GetMaxSpeed();
break;
case 4:
GetMinSpeed();
break;
case 5:
cout << "Good Bye" << endl;
break;
}
}
else if(x == 'N')
{
break;
}
}
return 0;
}
/*
In this menu function, it will ask the user to input the selection, ranging from 1 to 5.
If the user puts a number that is not between 1 to 5 or letters, then the program will
ask the user to input a valid selection.
*/
void menu()
{
int selection;
cout << "Selection Menu" << endl;
cout << "--------------" << endl;
cout << "\n1. Set Speed" << endl;
cout << "2. Get Speed" << endl;
cout << "3. Get MAX Speed" << endl;
cout << "4. Get MIN Speed" << endl;
cout << "5. Exit" << endl;
int bye = 0;
while(1)
{
cout << "\nYour selection :" <<endl;
cin >> selection;
bye = 0;
if((selection <= 5)&&(selection >= 1))
{
switch(selection)
{
case 1:
SetSpeed();
break;
case 2:
GetSpeed();
break;
case 3:
GetMaxSpeed();
break;
case 4:
GetMinSpeed();
break;
case 5:
cout << "Good Bye" << endl;
bye = -1;
break;
}
}
else
{
cout << "\nPlease input valid selection: " << endl;
cin >> selection;
switch(selection)
{
case 1:
SetSpeed();
break;
case 2:
GetSpeed();
break;
case 3:
GetMaxSpeed();
break;
case 4:
GetMinSpeed();
break;
case 5:
cout << "Good Bye" << endl;
bye = -1;
break;
}
}
if(bye == -1)
{
break;
}
}
}
int main()
{
menu();
return 0;
}//end of main function
This might serve your purpose. Call ask() as per your requirement if it didn't suit you.
#include <iostream>
#include <stdlib.h>
using namespace std;
char * title;
int a , b;
void menu();
void print(const char *c , int res )
{
cout<<"\n\n\n\n\nThe "<<c<<" of "<<a<<" and "<<b<<" is : " <<res<<endl;
}
void add()
{
print("Addition" , (a+b));
}
void sub()
{
print("subtraction" , (a-b));
}
void mul()
{
print("Multiplication" , (a*b));
}
void div()
{
print("Division" , (a/b));
}
void ask()
{
bool call_menu;
char ch;
cout<<"\n\n\n\n\n\n DO you Want to Continue? Y - N: ";
cin>>ch;
if(ch=='Y' || ch=='y')
{
call_menu= true;
}
else
{
if(ch=='N' || ch == 'n')
{
call_menu= false;
}
else
{
cin.clear();
ask();
}
}
if(call_menu)
{
system("clear"); // change this to system("cls") if on windows
menu();
}
else
{
system("clear"); // change this to system("cls") if on windows
cout<<"\n\n\n\n\n\n\n\t\t\tHave a Nice Day ! \n\n\n"<<endl;
}
}
void input(int *first , int *second)
{
system("clear"); // change this to system("cls") if on windows
cout<<"\n\n\n\t\t\t\t Calculator \n\n\n\n"<<endl;
cout<<"Enter the First Number : ";
cin>>(*first);
cout<<"\nEnter the Second Number :";
cin>>(*second);
}
void menu()
{
int ch;
cout<<"\n\n\n\t\t\t\t Calculator \n\n\n\n"<<endl;
cout<<"\n\n\t\t\t1 . Addition"<<endl;
cout<<"\n\n\t\t\t2 . Subtract"<<endl;
cout<<"\n\n\t\t\t3 . Multiply"<<endl;
cout<<"\n\n\t\t\t4 . Division"<<endl;
cout<<"\n\n\t\t\t5 . Exit" <<endl;
cout<<"\n\n\n\n Enter Your Choice : ";
cin>>ch;
if(ch >=1 && ch <5){
input(&a , &b);
}
switch(ch)
{
case 1:
add();
ask();
break;
case 2:
sub();
ask();
break;
case 3:
mul();
ask();
break;
case 4:
div();
ask();
break;
case 5:
exit(0);
break;
default:
system("clear"); // change this to system("cls") if on windows
cin.clear();
cin.ignore();
menu();
break;
}
}
int main(int argc, char **argv)
{
menu();
return 0;
}
Modify it as per your requirement.
There are several problems with the code in your question. The big problem is there is a lot of redundant code that can be easily eliminated by a few minor adjustments. You have both the menu printing and code to act on selections in several places. This is going to make managing the continue process a lot more difficult. By eliminating the redundant code and adjusting the logic in main and and menu you can not only reduce the complexity but make it far easier to manage.
For instance menu can be changed to remove the while loop and return a boolean value to indicate if the user wants to exit. This will allow you to select an option, act on it, then return letting other portions of the program handle asking the user if they want to continue.
The example below is a modification of your original code. It only addresses the logic for asking the user to continue and eliminates the redundant menu code. You should review the entire code and make additional adjustments as necessary.
#include <iostream>
#include <string>
using namespace std;
int sp;
int speed = 0;
int M, K, c, x;
const int MINspeed = 10;
const int MAXspeed = 40;
int GetSpeed()
{
char c;
while(true)
{
cout << "\nDo you want the speed in mph or km/h? \n"
<< "\nEnter M or K followed by Enter: " << flush;
cin >> c;
if( (c != 'M')&& (c != 'K'))
{
cout << "Incorrect Selection. Try Again! \n\n" << flush;
continue;
}
if ( c == 'M')
{
cout << "\nSpeed in mph: " << speed << endl;
return speed;
}
else if(c == 'K')
{
double toKmPerHour = 1.61;
double speedInKmPerHour = speed * toKmPerHour;
cout << "\nSpeed in km/h:" << speedInKmPerHour << endl;
return speed;
}
}
return 0;
}
int GetMinSpeed()
{
cout << "MIN speed = " << MINspeed << endl;
return 0;
}
int GetMaxSpeed()
{
cout << "MAX speed = " << MAXspeed << endl;
return 0;
}
void SetSpeed()
{
cout << "Input your speed: ";
cin >> speed;
}
/*
In this menu function, it will ask the user to input the selection, ranging from 1 to 5.
If the user puts a number that is not between 1 to 5 or letters, then the program will
ask the user to input a valid selection.
returns false if the user has selected the exit option
returns true for all other options
*/
bool menu()
{
cout << "Selection Menu" << endl;
cout << "--------------" << endl;
cout << "\n1. Set Speed" << endl;
cout << "2. Get Speed" << endl;
cout << "3. Get MAX Speed" << endl;
cout << "4. Get MIN Speed" << endl;
cout << "5. Exit" << endl;
int selection;
cout << "\nYour selection :" <<endl;
cin >> selection;
switch(selection)
{
case 1:
SetSpeed();
break;
case 2:
GetSpeed();
break;
case 3:
GetMaxSpeed();
break;
case 4:
GetMinSpeed();
break;
case 5:
cout << "Good Bye" << endl;
return false;
break;
default:
cout << "\nPlease input valid selection: " << endl;
}
return true;
}
int main()
{
for(bool process = true; process;)
{
process = menu();
if(process)
{
for(bool valid = false; !valid;)
{
cout << "\nDo you want to enter another selection? (Yes/No) " << flush;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string line;
getline(cin, line);
if(line == "No")
{
valid = true;
process = false;
}
else if(line == "Yes")
{
valid = true;
}
else
{
cout << "\nInvalid input\n\n" << flush;
}
}
}
}
return 0;
}//end of main function