Calculator

I've been trying to create a simple calculator, but whenever I fix an issue, another one appears. Was wondering if I could get a good scripted calculator, so I can learn from it.

Thank you.
Last edited on

post your code.
I want to see a scripted one if possible, so I can learn anything from it
If you posted your code then we could help you to fix any issues you are having with it. But if you really want to see an example then here's the example that I've written. I've added a bit of error handling too because I don't like writing code that breaks really easily!

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
#include <iostream>
#include <climits>
using std::cout;
using std::endl;
using std::cin;

double calc(char, double, double);

int main() {

    cout << "----------------------" << endl;
    cout << "Welcome to Calculator!" << endl;
    cout << "----------------------\n" << endl;

    char op; // the operation to do
    cout << "Enter an operation: ";
    cin >> op; // takes input for the operation
    cin.ignore(); // clear input buffer

    // check the operator is valid
    if (op != '+' && op != '-' && op != '*' && op != '/') {
        // invalid operator
        cout << "Invalid operator!" << endl;
        cin.ignore(); // pause the console before quitting
        return 0; // exit the program
    }

    double num1;
    double num2;

    cout << "Enter first number: ";
    cin >> num1; // get first number

    // check that they entered a valid number
    if (cin.fail()) {
        // they did not enter a valid number
        cin.clear(); // clear error flags from buffer
        cin.ignore(INT_MAX, '\n'); // clear everything in the input buffer
        cout << "Invalid number!" << endl;
        cin.ignore(); // pause the console
        return 0; // exit the program
    }
    cin.ignore(); // clear the input buffer


    cout << "Enter second number: ";
    cin >> num2; // get second number

    // check that they entered a valid number
    if (cin.fail()) {
        // they did not enter a valid number
        cin.clear(); // clear error flags from buffer
        cin.ignore(INT_MAX, '\n'); // clear everything in the input buffer
        cout << "Invalid number!" << endl;
        cin.ignore(); // pause the console
        return 0; // exit the program
    }
    cin.ignore(); // clear the input buffer

    cout << "Result: " << calc(op, num1, num2) << endl; // calculate the result and output it

    cin.get();
    return 0;
}

double calc(char op, double num1, double num2) {

    // determine the operation
    switch (op) {
    case '+':
        return num1 + num2;
    case '-':
        return num1 - num2;
    case '*':
        return num1 * num2;
    case '/':
        return num1 / num2;
    }

    return 0;
}
MOST basic calculator ever

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
#include <iostream>
using namespace std;
int main() {

		float num1;
		float num2;
		char character;

		cout << "|-----------------------|" << endl;
		cout << "|--Calculator v1.0--|" << endl;
		cout << "|-----------------------|" << endl;
		cin.ignore();

		start: //So we can go back to this part of the program

		cout << "First number: " << endl;
		cin >> num1;
		cin.ignore();

		cout << "Operation: " << endl;
		cin >> character;
		cin.ignore();

		cout << "Second number: " << endl;
		cin >> num2;
		cin.ignore();
		
		if(character == "+") {
				cout << num1 << "+" << num2 << "=" << num1+num2 << endl;
				goto start;
		}

		else if(character == "-") {
				cout << num1 << "-" << num2 << "=" << num1-num2 << endl;
				goto start;
		}

		else if(character == "*" || character == "x")
				cout << num1 << "x" << num2 << "=" << num1*num2 << endl;
				goto start;
		}

		else if(character == "/" || character == ":")
				cout << num1 << ":" << num2 << "=" << num1/num2 << endl;
				goto start;
		}

		else {
				cout << "Invalid command, please try again" << endl;
				goto start;
		}
}


I hope you enjoy my simple basic (really basic) Calculator code \o/
Well, alright. Here's 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
int main(int argc, char **argv)
{
    double x[50] = { };
	double natg=0;
	char yesawy = '\0';
	for(int i=0; i<50; i++)
	{
		cout << "Enter Number or \"=\"" << endl;
		cin >> x[i];
		natg += x[i];
		if(cin.good())
		{
			continue;
		}
		else
		{
			cin.clear();
			cin >> yesawy;
			if(yesawy == '=')
			{
				cout << natg;
				break;
				
			}
			else
			{
				cout << "invalid char";
				i--;
			}
		}
	}
}


When I enter a few characters in the number, it spams the program. If I enter 5 characters(for example), it repeats the else 5 times, idk why. Here's a screenshot of what I mean.
http://s21.postimg.org/cf3dv933b/Screenshot_2730.png

How do I fix this?
This problem can be fixed by adding the following line somewhere inside the else statement between lines 25 and 29.

cin.ignore(INT_MAX, '\n');

This happens because you don't clear what's left in the input buffer. You take a single character as input but then there are still characters left in the input buffer which are then retrieved the next time you take input. This line just clears everything inside the input buffer.

You may need to add #include <climits> somewhere too so you can use INT_MAX.
Last edited on
Thanks Tom56785, it worked. Appreciate it.

How could I also add an IF statement to check whether there are characters at the same line or not. Because at the moment, if I add "234jfdsi;jmfasd", it will take 234 and ignores the other characters, I want it to give an error message or something. Here's another screenshot.
http://s4.postimg.org/mf7cabqst/Screenshot_2731.png
One method is to check the next character of the input buffer without actually retrieving it, if the character is not an endline ('\n') character then they entered characters that weren't numbers.

Here's the new snippet of code; I moved one of the lines into the 'if' statement and added another condition.

1
2
3
4
5
6
7
8
9
10
11
cout << "Enter Number or \"=\"" << endl;
cin >> x[i];

if(cin.good() && cin.peek() == '\n')
{
    natg += x[i];
    continue;
}
else
{
...
Last edited on
Topic archived. No new replies allowed.