if and else help

For my calculator program my if else statement defaults to the no result even if I type yes. Why is that?

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
  #include <iostream>
int yes, no;
char symbol;

int main()
{
	std::cout << "Welcome to my calculator\n"
		<< "Would you like to use the calculator?\n"
		<< "Please enter yes or no\n";
	std::cin >> yes, no;
	if (yes)
	{
		std::cout << "Please enter a symbol to indicate which type of problem we will be solving\n"
			<< "The choices are +, -, /, *, or % to find the modulus of two numbers\n";

		std::cin >> symbol;

		if (symbol == '+')
		{
			double number1, number2;
			/* First draft had number1 and number2 declared as integers.
			This didn't allow for decimals so I've changed it.*/
			std::cout << "Please enter two numbers\n";
			std::cin >> number1 >> number2;

			std::cout << number1 << " plus " << number2 << " is " << number1 + number2 << '\n';
		}

		else if (symbol == '-')
		{
			double number1, number2;
			std::cout << "Please enter two numbers\n";
			std::cin >> number1 >> number2;


			std::cout << number1 << " minus " << number2 << " is " << number1 - number2 << '\n'
				<< number2 << " minus " << number1 << " is " << number2 - number1 << '\n';
		}

		else if (symbol == '/')
		{
			double number1, number2;
			std::cout << "Please enter two numbers\n";
			std::cin >> number1 >> number2;

			std::cout << number1 << " divided by " << number2 << " is " << number1 / number2 << '\n'
				<< number2 << " divided by " << number1 << " is " << number2 / number1 << '\n';
		}

		else if (symbol == '*')
		{
			double number1, number2;
			std::cout << "Please enter two numbers\n";
			std::cin >> number1 >> number2;

			std::cout << number1 << " times " << number2 << " is " << number1 * number2 << '\n';
		}

		else if (symbol == '%')
		{
			int number1, number2;
			std::cout << "Please enter two integers\n";
			// Can only use whole numbers for modulus
			std::cin >> number1 >> number2;

			if (number2 != 0) std::cout << number1 << " modulus " << number2 << " is " << number1 % number2 << '\n';
			if (number1 != 0) std::cout << number2 << " modulus " << number1 << " is " << number2 % number1 << '\n';
		}

		else
		{
			std::cout << "\nInvalid operator entered\n";
		}
	}
	else
		std::cout << "Goodbye\n";
}
int yes ← yes is an int. It can only hold integers. If you will try to insert letters into it it will put stream into failed state and value of yes would be either 0 (false) or some unspecified value depending on C++ standard used.
std::cin >> yes, no; is equivalent to std::cin >> yes and should emit a warning (right hand operand of comma has no effect)
Topic archived. No new replies allowed.