Hello MaxGreen,
Here is a little different approach to your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
//#include <cstdlib> // <--- "srand()" and "rand()".
//#include <ctime>
using namespace std;
int main()
{
//srand(time(0));
//srand(static_cast<unsigned int>(time(nullptr)));
int a{}, b{}, c{}, result{};
int menuChoice{};
M:
system("cls");
cout <<
"\n\t Menu Choice\n"
"\t-----------------------\n"
"\t 1. Enter a\n"
"\t 2. Enter b\n"
"\t 3. Enter c \n"
"\t 4. Calculate a+b-c\n"
"\t 5. Check the result\n"
"\t 6. Exit\n"
"\t Choose from 1 to 6 :: ";
cin >> menuChoice;
cout << endl; // <--- Could be written as: "cout << '\n';".
switch (menuChoice)
{
case 1:
cout << " Choose a value for a: ";
cin >> a;
cout << " a = " << a << endl;
break;
case 2:
cout << " Choose a value for b " << endl;
cin >> b;
cout << " b= " << b << endl;
break;
case 3:
cout << " Choose a value for c " << endl;
cin >> c;
cout << " c= " << c << endl;
break;
case 4:
if (result = a + b - c)
cout << " Calculate the result for a+b-c, which is equal to " << a + b - c << endl;
else
cout << "Choose a,b,c" << endl;
break;
case 5:
if(result)
cout << "Result is " << result << endl;
else
cout << "Choose option 4 to calculate the equation " << endl;
break;
case 6:
goto exit;
default:
cout << " Impossible option, choose wisely " << endl;
}
//system("pause");
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
goto M;
exit:
return 0;
}
|
I have not idea what IDE/compiler you are using.
For your include files. Do not count on "iostream" including the header file "cstdlib" because it may not. VS does include the header file "xlocnum" which then includes:
1 2 3 4 5
|
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <streambuf>
|
but the MIN GW compiles does not include these header files. So it is better to include the header files that you need and let the header guards keep something from being included more than once.
The 2 header files that are commented out are because you do not need or use them.
In "main" line 10 is a better way to write "srand". Looking at "srand" the parameter requires an "unsigned int" for its variable type, but "time" does not return an "unsigned int" hence the type case. From C++11 on "nullptr" is the better choice over "0" or "NULL". And if you are going to use "rand" this video is worth watching:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful At least it will give you a better idea of how you can use "rand".
Since you never use "rand()" in the program these lines are commented out.
Next it always a good idea to initialize your variables when they are defined. From C++11 on you have the uniform initializer of the {}s. Empty they will set the variable to zero based on the variables' type. You can also put anything needed between the {}s to initialize the variable to a given value.
Next would be to give the variables a better name other than a single letter. AS you progress you will find this to be a much greater benefit when coding because it is easier to understand what the variables are.
Your 2 lines using "system" should be avoided as this can leave your program open to hackers who could gain access to your computer. If you are interested I have a function that will clear the screen and can show you how to use it. For the pause I use this:
1 2 3 4 5
|
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
|
For the menu I have found this to be helpful. The way it is laid out usually gives a good representation of what it will look like on the screen. Since the "cout" can be chained together you only need 1. Making use of the "\n"s eliminates all the "endl"s which create overhead and cause a program to run slower than needed.
The other part that you can use to your advantage is for example lines 19 - 28 is 1 "cout" statement, but the "cin" on line 29 will flush the output buffer and print the entire "cout" to the screen before any input is allowed. So if the "\n"s do not flush the output buffer the "cin" will. Save the "endl"s for when you really need them.
I added the 6th choice because there was no way to end the program.
For the most part the "switch" is OK. I did add the "case 6" to end the program.
I have not worked on it yet, but againtry's idea of the bool variables is a possible solution as to whether you have a value for your variables or need to enter values.
In "case 4" the if statement is good, but you are going about it the wrong way. Consider "1 + 2 - 3" the result would be zero which would cause the if statement to be false and execute the else part even if it should not. Using the bool variables you could do something like this:
1 2 3 4 5 6 7 8 9 10
|
case 4:
if (a_selected && b_selected, && c_selected)
{
result = a + b - c;
cout << " Calculate the result for a+b-c, which is equal to " << result << endl;
}
else
cout << "Choose a,b,c" << endl;
break;
|
I have not tested this yet, but it should work.
The same idea would work for "case 5", but it would be best if you have a 4th bool variable to show that you have actually done step 4. Again because "result" could be zero.
For the "goto" part and forgive mu bluntness, but the person teaching this should be shot, drawn and quartered and have a wooden stake driven into their heart.Although the "goto" statement had a limited use it is bad form for general use, as done here. A do/while or while loop can replace the "goto".
The "goto" is an unconditional jump to its label and does not care about where it came from or if it would cause a problem.
I realize you may not have learned about loops yet, but understand the "goto" is not the best way to program.
Andy