Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help running the result

Status
Not open for further replies.

adam143

Programmer
Apr 29, 2010
3
CA
I'm learning c language using Dev-c++;when I write the source program compile it and run it it went well except it does not feature the last result .exemple:sum calculation.

here the outcome:

enter the first enteger :77
enter second enteger :89
here the problem : when I press enter in my keyboard I do not have the sum result but it returns to the source code !

Could someone help me .thank you very much
 
You probably need code to stop the window from closing.

Try adding cin.get(); to your code just before the end (before any return calls). You will actually probably need to do it twice.
Code:
int main()
{
  // ... all your code

  std::cin.get();
  std::cin.get();

  return 0;
}
 

Thanks uolj ;could you check for me this code.i still have the problem with the final result.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int x, y;
cout << "Enter two entegers: ";
cin >> x >> y;
cout << "Sum of " << x << "and " << y << " is: "
<< ( x + y ) << endl;

return 0;

}
 
Try adding the calls to cin.get() like I showed you. Does it work then?
 
Beautifull you are the best!Thanks a lot I greatly appreciate your help
 
You're welcome.

The problem is just a result of the way Dev-C++ (and many other IDEs) run your program. It creates a console window and runs your program in it. But when your program completes, the window isn't needed anymore so it closes. Unfortunately, that means it closes before you can see the final output.

The call to cin.get() makes the code wait for the user to type something and hit enter. So the window stays open waiting for the user to hit enter, and only then is the program finished so the window can close.

It needs two calls to cin.get() because of a quirk in the way cin reads input. When the user types 89 and hits <enter>, the first call to cin.get() grabs that <enter>, so you need another call to keep the window open.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top