Hello majkel1234,
Working with your original code:
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
|
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others. Added.
#include <iostream> // <--- Changed.
#include <iomanip> // <--- Added.
#include <string>
using namespace std;
int main(int argc, char** argv)
{
double firstNum{}, secondNum{};
double wynik{};
string mojwybor; // <--- Would work better as a "char" type.
std::cout << std::fixed << /*std::showpoint <<*/ std::setprecision(3); // <--- "showpoint" is optional if "setprecision"
// is > zero. Needed for floating point numbers. You could also do without this line.
do
{
cout <<
"\n"
"|==============================================|\n"
"| 4 basic mathematical operations |\n"
"|==============================================|\n"
"| choose a - addition |\n"
"| choose b - subtraction |\n"
"| choose c - multiplication |\n"
"| choose d - division |\n"
"| choose q- to finish |\n"
"|==============================================|\n"
"choose option: ";
cin >> mojwybor;
mojwybor[0] = std::tolower(mojwybor[0]);
if (mojwybor[0] != 'a' && mojwybor[0] != 'b' && mojwybor[0] != 'c' && mojwybor[0] != 'd' && mojwybor[0] != 'q')
{
std::cerr << "\n Invalid choice! try again.\n";
}
} while (mojwybor[0] != 'a' && mojwybor[0] != 'b' && mojwybor[0] != 'c' && mojwybor[0] != 'd' && mojwybor[0] != 'q');
if (mojwybor == "q")
{
return 0;
}
cout << "\nchoose first mummber = ";
cin >> firstNum;
cout << "choose second number = ";
cin >> secondNum;
if (mojwybor == "a")
{
wynik = firstNum + secondNum;
cout <<
"\n"
"|==============================================|\n"
//"|you choose addition of firstNum + secondNum =" << wynik << "\n"
"|you choose addition of " << firstNum << " + " << secondNum << " = " << wynik << "\n"
"|==============================================|\n";
}
else if (mojwybor == "b")
{
wynik = firstNum - secondNum;
cout << "|==============================================|" << endl;
cout << "|you choose substraction of firstNum- secondNum =" << wynik << endl;
cout << "|==============================================|" << endl;
}
else if (mojwybor == "c")
{
wynik = firstNum * secondNum;
cout << "|==============================================|" << endl;
cout << "|you choose multiplication of firstNum * secondNum =" << wynik << endl;
cout << "|==============================================|" << endl;
}
else if (mojwybor == "d")
{
// <--- Should check that "secondNum" is not zero. Or Do it after you enter the second number.
wynik = firstNum / secondNum; // <--- Could be a runtime divide by zero error.
cout << "|==============================================|" << endl;
cout << "|you choose division of firstNum / secondNum =" << wynik << endl;
cout << "|==============================================|" << endl;
}
return 0; // <--- Not required, but makes a good break point.
}
|
Note the comments in the code.
The include
#include <cstdlib>
really does not do much for you, but you do need
#include <iostream>
for the "cin"s and "cout"s.
Prefer to use the new line "\n" over the "endl" as "endl" is a function that takes time. The more "endl"s you have the more time it takes.
"double" is the preferred floating point type.
Also, as a quick example:
1 2
|
std::cout << "something\n";
std::cin >> aVariable;
|
The "cin" will flush the output buffer before taking any input. Unfortunately it does not work in reverse.
Avoid using global variables. It may appear easy right now, but in the future it
WILL be a problem. In addition these variables are only used inside main, so there is no reason for them to be global.
Global variables that start with "const" or "constexpr" are OK because they can not be changed in the program.
For lines 19 - 30 the quoted strings are considered 1 large string by the "cout", but it does give you a better representation of what the output will look like. A "cout" for every line is not necessary.
Line 31 allows you to enter a string. Not the best idea, but it does work.
Line 33 makes sure the first element of the string is in lower case otherwise the if condition and while condition would need to be twice as big to account for capital letters.
Also notice that I used the first element of the string and not the whole string. Using the whole string, if it contains more than 1 character, would add more work to the program.
Changing "mojwybor" to a "char" variable would eliminate the need for "mojwybor[0]" to compare only the 1st element.
I moved the else statement at the end of the program to line 41 because you need to check for "q" before the program continues. There is no reason to have to enter 2 numbers just to end the program.
I only worked with addition part, so see what you think of what I did. IMHO I think it looks better in the output.
In the divide part you need to make sure that you are not dividing by zero which will cause a run time error.
Andy