Basic Calculator - How to terminate the program?

hi, I am trying to write some code to create a basic calculator. That is it will read a number, read an operator, read another number, then do the operation. The calculator works with integers and uses four functions: +, -, *, and /. After the first operation is completed, the program will read another operator and uses the result of the previous operation as the first value for the next operation. If the user enters a C the result is cleared and then the user starts entering a new number. If the user enters an X, the calculator is turned off. I haven't gotten to the clearing part yet or how to get the calculator to loop the calculation with the solution for the last calculation to act as the first for the next calculation. I am having troubles getting the program to terminate at the user input x.

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


int _tmain(int argc, _TCHAR* argv[])
{
	int firstnumber, solution;
	int secondnumber;
	char operand;

	cout << "This program is a basic calculator!" << endl;
	cout << "It performs multipication, division, addition, and subtraction." << endl;
	cout << "After the first operation is completed," << endl;
	cout << "the program will read another operator and use the result of the previous operation" << endl;
	cout << "as the first value for the next operation." << endl;
	cout << "Enter 'C' to clear your calculator and 'X' to exit the program" << endl;

	
		cout << "To begin please enter your first number " << endl;
		cin >> firstnumber;
		cout << "please enter an operand (+,-,*,/) " << endl;
		cin >> operand;
		cout << "Enter your second number" << endl;
		cin >> secondnumber;
		while (firstnumber != 'X' || firstnumber != 'x' || secondnumber != 'X' || secondnumber != 'x');
		{
		if(operand == '+')
		{
			solution = firstnumber + secondnumber;
		}
		else if (operand == '-')
		{
			solution = firstnumber - secondnumber;
		}
		else if (operand == '*')
		{
			solution = firstnumber * secondnumber;
		}
		else if (operand == '/')
		{
			solution = firstnumber / secondnumber;
		}		
			cout << firstnumber << operand << secondnumber << " = " << solution << endl;
		}

		if (firstnumber == 'X' || firstnumber == 'x')
		{
			cout << "You entered 'X' - the program will now end ... goodbye." << endl;
			return 0;
		}
		else if (secondnumber == 'X' || secondnumber == 'x')
		{
			cout << "You entered 'X' - the program will now end ... goodbye." << endl;
			return 0;
		}
	
		return 0;
	
}
On line 28, change each || to &&. Since firstnumber cannot equal both 'X' and 'x' at the same time, your while condition is always yielding true.
closed account (G30GNwbp)
try:
while (!(firstnumber == 'X' || firstnumber == 'x' || secondnumber == 'X' || secondnumber == 'x'))

edited to correct error pointed out by booradley
Last edited on
Your problem is that you're asking cin to parse an integer value from the input. If the user enters 'X', it isn't parsed at all by cin and just left on the stream. If you want your feature to work, you will have to parse the input as a string and then later convert it to an integer if X is not detected.

Actually, another problem is that you have a semicolon on the end of line 28. That specifies an empty loop, and you'll just be there for infinite time if your while conditions are met.
Last edited on
rtd2645 is incorrect. He's reversed the problem from "the loop always executes" to "the loop never executes".
closed account (G30GNwbp)
looks like booradley is right; so here is something that is not good but it is workable.
I believe that one of your real problems is line 28 (that semicolon is causing you problems)

Try running your compiler with both -Wall and -Wextra

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
#include <iostream>
#include <string>
#include <sstream>

int str_int (std::string& s)
{
    std::stringstream ss{s};
    int val;
    ss >> val;
    return val;
}
   


int main()
{
    std::string firstnumber;
    std::string secondnumber;
    std::string operand;


    while (!(firstnumber == "X" || firstnumber == "x" || secondnumber == "X" || secondnumber == "x"))
    {
        std::cout << "To begin please enter your first number " << std::endl;
        std::cin >> firstnumber;
        std::cout << "please enter an operand (+,-,*,/) " << std::endl;
        std::cin >> operand;
        std::cout << "Enter your second number" << std::endl;
        std::cin >> secondnumber;
        if (operand == "+")
            std::cout << str_int(firstnumber)  << " + " << str_int(secondnumber)  << " = " << (str_int(firstnumber) + str_int(secondnumber)) << std::endl;
        else if (operand == "*")
            std::cout << str_int(firstnumber)  << " * " << str_int(secondnumber)  << " = " << (str_int(firstnumber) * str_int(secondnumber)) << std::endl;
        else if (operand == "/")
            std::cout << str_int(firstnumber)  << " / " << str_int(secondnumber)  << " = " << (str_int(firstnumber) / str_int(secondnumber)) << std::endl;
        else if (operand == "-")
            std::cout << str_int(firstnumber)  << " - " << str_int(secondnumber)  << " = " << (str_int(firstnumber) - str_int(secondnumber)) << std::endl;
        else
            std::cout << "bad operand try again\n";
    }

    if ((firstnumber == "X" || firstnumber == "x" || secondnumber == "X" || secondnumber == "x"))
    {
        std::cout << "You entered 'X' - the program will now end ... goodbye." << std::endl;
        return 0;
    }

    return 0;

}
Topic archived. No new replies allowed.