Originally posted by absenta
hello, this is my first try of math operation with variables:
#3include <iostream.h>
int main()
{
// declare variables
int firstNumber;
int secondNumber;
int result = firstNumber + secondNumber;
//read variables content
cout<<"please enter 1st number:";
cin>>firstNumber;
cout<<"enter 2nd number:";
cin>>secondNumber;
// get the result of a + b
count<< "you get: result";
//here's my problem, i just can introduce firstnumber valor, then window closes. I use
cin.get();
result 0;
}
So, how can i mantain the windows open, or maybe let press enter without closing window.
Thanks.
Well a real cheesy way to keep the window open is, at the bottom of your code right before result 0 (also this should be return 0) you can put
system( "pause" );
Which sends the command 'pause' to DOS. Otherwise you can #include <conio.h> and use getch(); and the program will wait for a key input. Also, when you're initializing result just assign it to 0. Later in the program after you have gotten the user's 2 variables, then say result = firstNumber + secondNumber; Because right now the way you have it, the compiler assigns whatever random thing is in memory for firstNumber and secondNumber to result. So.. perhaps it should look something like this...
Code:
#include <iostream>
using namespace std; // For new streams
int main()
{
// declare variables
int firstNumber = 0;
int secondNumber = 0;
int result = 0;
//read variables content
cout<<"please enter 1st number:";
cin>>firstNumber;
cout<<"enter 2nd number:";
cin>>secondNumber;
// get the result of a + b
result = firstNumber + secondNumber;
count<< "you get " << result << endl;
// Either....
system( "pause" );
// Or... (but not both ..)
cout << "Press any key to continue" << endl;
getch();
return ( 0 );
}
Good Luck!