calculator

LOOK AT MY 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
 
#include <cstdlib>
#include <string>

using namespace std;

float m = 0, n = 0;
float wynik = 0;
string mojwybor;

int main(int argc, char** argv)
{
    cout << "|        4 basic mathematical operations       |" << endl;
    cout << "|==============================================|" << endl;
    cout << "|      choose   a - addition                   |" << endl;
    cout << "|      choose   b - subtraction                |" << endl;
    cout << "|      choose   c - multiplication             |" << endl;
    cout << "|      choose   d - division                   |" << endl;
    cout << "|      choose   q-  to finish                  |" << endl;
    cout << "|==============================================|" << endl;
    cout << "choose option:";
    cin >> mojwybor;
    cout << "choose first mummber m = ";
    cin >> m;
    cout << "chsecond number n = ";
    cin >> n;

    if (mojwybor == "a") {
        wynik = m + n;
        cout << "|==============================================|" << endl;
        cout << "|you choose addition of m+n =" << wynik << endl;
        cout << "|==============================================|" << endl;
    }
    else if (mojwybor == "b") {
        wynik = m - n;
        cout << "|==============================================|" << endl;
        cout << "|you choose substraction of m-n =" << wynik << endl;
        cout << "|==============================================|" << endl;
    }
    else if (mojwybor == "c") {
        wynik = m * n;
        cout << "|==============================================|" << endl;
        cout << "|you choose multiplication of m*n =" << wynik << endl;
        cout << "|==============================================|" << endl;
    }
    else if (mojwybor == "d") {
        wynik = m / n;
        cout << "|==============================================|" << endl;
        cout << "|you choose division of m/n =" << wynik << endl;
        cout << "|==============================================|" << endl;
    }
    else if (mojwybor == "q") {
    }
    else {
        cout << "|====================================|" << endl;
        cout << "|please use a,b,c,d lub q !|" << endl;
        cout << "|====================================|" << endl;
    }
}

I WAS SEARCH 1/5 OF NET FOR HELP :) UNFORTUNATELY NO HELP :(
MY PROBLEM IS :
I NEED TO SHOWN MESSAGE "CHOOSE: A, B, C, D OR Q
WHEN USER WILL CHOOSE A DIFFERENT LETTER THAN A,B,C,D OR Q
Last edited on
it seems to work:

choose option:x
choose first mummber m = 4
chsecond number n = 5
|====================================|
|please use a,b,c,d lub q !|
|====================================|

but it is out of order a little. you can re-write it (re-arrange will not work) so that it checks first, then gets M and N only if the input made sense (abcdq) if you want.
to hard for me, i need to know there ui need to put it.
Turn the volume down.
http://www.catb.org/esr/faqs/smart-questions.html#writewell

Start learning to write small functions that perform specific tasks.
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
void showOptions() {
  cout << "|        4 basic mathematical operations       |" << endl;
  cout << "|==============================================|" << endl;
  cout << "|      choose   a - addition                   |" << endl;
  cout << "|      choose   b - subtraction                |" << endl;
  cout << "|      choose   c - multiplication             |" << endl;
  cout << "|      choose   d - division                   |" << endl;
  cout << "|      choose   q-  to finish                  |" << endl;
  cout << "|==============================================|" << endl;
}

string chooseOption() {
  string mojwybor;
  cout << "choose option:";
  cin >> mojwybor;
  return mojwybor;
}

void inputValues(float &m, float &n) {
  cout << "choose first mummber m = ";
  cin >> m;
  cout << "choose second number n = ";
  cin >> n;
}

void calculator() {
  float m = 0, n = 0;
  float wynik = 0;
  inputValues(m,n);
  if (mojwybor == "a") {
    wynik = m + n;
    cout << "|==============================================|" << endl;
    cout << "|you choose addition of m+n =" << wynik << endl;
    cout << "|==============================================|" << endl;
  }
  //etc
}

int main(int argc, char** argv)
{
  // no global variables
  string mojwybor;

  showOptions();
  mojwybor = chooseOption();

  if ( mojwybor == "a" || mojwybor == "b" ) {
    calculator();
  }
}



Something like this - but note salem's comment re using functions:

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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	const string valid {"abcdq"};

	do {
		float m = 0, n = 0;
		float wynik = 0;
		string mojwybor;

		cout << "|        4 basic mathematical operations       |" << endl;
		cout << "|==============================================|" << endl;
		cout << "|      choose   a - addition                   |" << endl;
		cout << "|      choose   b - subtraction                |" << endl;
		cout << "|      choose   c - multiplication             |" << endl;
		cout << "|      choose   d - division                   |" << endl;
		cout << "|      choose   q-  to finish                  |" << endl;
		cout << "|==============================================|" << endl;

		do {
			cout << "choose option:";
			cin >> mojwybor;

			if (valid.find(mojwybor) == string::npos) {
				cout << "|====================================|\n";
				cout << "|please use a,b,c,d or q !|\n";
				cout << "|====================================|\n";
			} else
				break;
		} while (true);

		if (mojwybor == "q")
			break;

		cout << "choose first number m = ";
		cin >> m;

		cout << "choose second number n = ";
		cin >> n;

		if (mojwybor == "a") {
			wynik = m + n;
			cout << "|==============================================|" << endl;
			cout << "|you choose addition of m+n =" << wynik << endl;
			cout << "|==============================================|" << endl;
		} else if (mojwybor == "b") {
			wynik = m - n;
			cout << "|==============================================|" << endl;
			cout << "|you choose subtraction of m-n =" << wynik << endl;
			cout << "|==============================================|" << endl;
		} else if (mojwybor == "c") {
			wynik = m * n;
			cout << "|==============================================|" << endl;
			cout << "|you choose multiplication of m*n =" << wynik << endl;
			cout << "|==============================================|" << endl;
		} else if (mojwybor == "d") {
			wynik = m / n;
			cout << "|==============================================|" << endl;
			cout << "|you choose division of m/n =" << wynik << endl;
			cout << "|==============================================|" << endl;
		}
	} while (true);
}

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
Topic archived. No new replies allowed.