I am writing a simple GPA Calculator program for my C++ class. We just learned if else statements this week, so that is what the program does a lot of. If the user does not enter either an A, B, C, D, or F I want it so the program just stops there. How would I do that? I just need something that terminates the program as soon as that user error occurs. If you need any of my source code please let me know.
returnwill exit a function. In this case, it will exit the main(). Yes, you can have multiple returnstatements. When the first one is reached, the function exists. When the main() function exits, the program ends.
It's the same as calling a function with a lot of code in it. If you start the function and a condition isn't met, you don't want to run all the code, so you return;.
1 2 3 4 5 6 7
void func( bool d )
{
if( ! ( d ) )
return;
//Load of code here that you don't want to execute if the above is incorrect.
}
Since you are limited to if else statements I would do the following:
declare variables
char getInput;
write a cout statement asking for user to type a, b, c, d, or f
cin>>getInput;
if (getInput == 'a')
do something
else if (getInput == 'b')
do something
else if(getInput == 'c')
do something
else if(getInput == 'd')
do something
else if(getInput == 'f')
do something
else
return 0;