hey i'm new to c++ , i am making a program which uses switch case, can anyone tell me how should I code this switch part so that whatever choice I enter calls the corresponding function (or prints the output related to that particular choice) and not the choice I have entered.
This is like the most basic C++ stuff there is.. I made a basic template of what you need and you can edit the code yourself. If this is for school though you should really understand how it works, it's not that complicated. I commented it all for you.
#include <iostream> //Include "input output stream" to use cout and cin functions.
usingnamespace std; //So we don't have to do std::cout/cin every time we use it
int main()
{
//All an enum does is take your variables and apply a number to them.. if you
//remove =1 after FirstChoice then it will start at 0 instead of 1
enum Choice { FirstChoice = 1, SecondChoice, ThirdChoice, FourthChoice };
//Sets up a new int variable named myChoice
int myChoice;
//Outputs "enter your choice" to the console and then inputs your response to myChoice
cout << "Enter your choice: ";
cin >> myChoice;
//Starts the switch statement for the myChoice variable
switch(myChoice)
{
//Remember FirstChoice was set to "1" in our enumeration, so if you enter 1
//then this code executes.. the break; line ends the switch
case FirstChoice:
cout << "You entered 1." << endl;
break;
case SecondChoice:
cout << "You entered 2." << endl;
break;
case ThirdChoice:
cout << "You entered 3." << endl;
break;
case FourthChoice:
cout << "You entered 4." << endl;
break;
//The default code will come up if you enter any number other than the ones above
default:
cout << "ERROR! You entered an invalid number." << endl;
break;
}
return 0;
}