-1

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 by !nsane: n/a

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by !nsane
Featured Replies
  • 1
    WaltP 2,905   6 Years Ago

    [ICODE]1.) system("PAUSE"); 2.) system("PAUSE>NUL");[/ICODE] Neither are recommended... [ICODE]3.) cin.ignore(2); [/ICODE] Useless without [ICODE]cin.get()[/ICODE] to read the character to enter. Use a combination if [ICODE]cin.ignore[/ICODE] followed by [ICODE]getline[/ICODE] to pause your code. By the way, software does not [I]collapse[/I]. Please explain what you really see in detail otherwise all we are … Read More

0

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
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.

0

Then i press RETURN and the program collapses.

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.