calculator with while loop

Hey guys, I'm programming a calculator in my c++ class and the assignment is as follows: Write a program, which will act as a simple four-function 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. The various input values (i.e. numbers, operators, commands) will be followed by the ENTER key. Your program should prompt the user on what the user is to do. The commands C and X may be entered in place of an operator.

This what I got so for but my problem is with the getting the total/result to display.

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>
#include <stdlib.h>
using namespace std;

void main()

{
	int		num1;
	int		num2;
	char	op;
	int		result = 0;

	bool	flag = true;

	cout << "Enter first number: ";
	cout << "Press enter key following the number ";
	cin >> num1;

	while (flag)
	{
		cout << "Enter operator +, -, *, /, X for turn off, or C for clear back to zero: ";
		cout << "Press enter key following the operator: ";
		cin >> op;

		if ((op == 'X') || (op == 'x'))
		{
			cout << "Bye: ";
			exit(0);
		}
		else if ((op == 'C') || (op == 'c'))
		{
			cout << result << " Enter first number: ";
			cin >> num1;
		}
		else if ((op == '+') || (op == '-') || (op == '*') || (op == '/'))
		{
			cin >> op;
			cout << "Enter second number: ";
			cout << "Press enter key following the number ";
			cin >> num2;

		}
		else if ((op != '+') || (op != '-') || (op != '*') || (op != '/'))
		{
			cout << "Error, please enter correct operator: ";
		}

		

	}
	
}
Hello jaitoine,

I think you need to pick your instructions apart into smaller pieces because you are missing some important parts in your program. Like you do no actual calculations with what you enter and a choice of "C" clears nothing, but you ask for the first number when you should be using the result of the last calculation for the first number.

Two small things I see: line 5 should be int main(). "void" is no longer an accepted return value for "main". And line 51 is missing return 0;.

Hope that helps,

Andy
As Handy Andy pointed out, you are not actually doing any calculations. I might suggest a switch statement:
41
42
43
44
45
46
  switch (op)
  {
  case '+':  // Do addition
                 break;
  //  Repeat for other operators
  }


Line 43: Your boolean logic is faulty. If op is '+', then op != '-' will be true making the entire condition true. In fact, the if portion is not needed since you've already checked the valid operators at line 35. Only the else is needed.
Topic archived. No new replies allowed.