Problems with console

Hi. I'm an -absolute newbie- and I'm trying to write a calculator in C++. I use Dev++.

I currently have the code (below) that asks the user for 2 numbers and an operation and adds 'em up, simple right? Well, whenever I run the program and type in whatever, it just closes.

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
#include <iostream>
#include <cmath>
#include <cstdio>

int main()
{
  using namespace std;
 
  int Number1;
  int Number2;
  string Operation;
 
  cout << "Welcome to the calculator!" << endl;
  cout << "Enter your first number: ";
  cin >> Number1;
  cout << "Enter your second number: ";
  cin >> Number2;
  cout << "Operation (*,/,+,-): ";
  cin >> Operation;
 
  if (Operation == "+"){
     cout << "The result is: " << Number1 + Number2;}
        
 getchar();  
 return 0; 
}   


I've tried system("pause") , getchar() and cin.get() and NOTHING WORKS! If someone could please look at my code and tell me what I'm doing wrong, I would really appreciate it!
I think that your getchar reads new line symbol after the last input of Operation.
You can for example insert one more getchar.
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
#include <iostream>
#include <string>

int main()
{
	
 
  int Number1;
  int Number2;
  std::string Operation;
 
 std::cout << "Welcome to the calculator!" << std::endl;
 std::cout << "Enter your first number: ";
 std::cin >> Number1;
 std::cout << "Enter your second number: ";
 std::cin >> Number2;
 std::cout << "Operation (*,/,+,-): ";
 std::cin >> Operation;
 
  if (Operation == "+"){
     std::cout << "The result is: " << Number1 + Number2<<std::endl;}
        
  std::cin.ignore();
  std::cout<<"Press Enter Key To Exit";
  std::cin.ignore();
 return 0; 
} 
Last edited on
Topic archived. No new replies allowed.