Hi!
I'm just started learning C++ and i tried to do a calculator for fun.
I did not succeed :$
There is an error here that i cannot find... Please, if you find it let me know :icon_smile:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

double num1;
double num2;
char sign;

cout << "Once you enter a number press \"ENTER\"" << endl;
cin >> num1;
cin >> sign;
cin >> num2;
cout << "The result is ";
if (sign == '+'){
      cout << num1+num2;
}
else if(sign == '-'){
      cout << num1-num2;
}
else if(sign == '*'){
     cout << num1*num2;
}
else if(sign == '/'){
     cout << num1/num2;
     }
else{
cout <<"Please enter +,-,* or /";
}
return 0;
}

Every time I run the program I type (example):

5
+
5

Then i press RETURN and the program collapses.

Edited 6 Years Ago by !nsane: n/a

right before your return 0; you need to put a line of code to pause your program.

examples:

1.) system("PAUSE");
2.) system("PAUSE>NUL");
3.) cin.ignore(2);

1.) system("PAUSE");
2.) system("PAUSE>NUL");

Neither are recommended... 3.) cin.ignore(2); Useless without cin.get() to read the character to enter.

Use a combination if cin.ignore followed by getline to pause your code.


By the way, software does not collapse. Please explain what you really see in detail otherwise all we are doing is guessing -- which is all these two posts are doing anyway.

Then i press RETURN and the program collapses.

By collapses I mean : the software dissapears from the screen, it closes by itself

This article has been dead for over six months. Start a new discussion instead.