By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Window closes immediately after execution.

P: 1
k, so here's the problem: this is the code i've written:


#include <iostream>
using namespace std;

int main()

{
int length, width;

cout << "Enter the length: ";
cin >> length;

cout << "Enter the width: ";
cin >> width;

cout << "The area is " << length * width;

return 0;
}

But it's probably not even my code since this works for anything i write.
When i compile and run this file in Dev C++, i can enter the first two digits no problem, but when i hit enter after that, it doesnt display my area, or it might but, the windows closes instantly.

Anyone know how to fix this, is it simply some settings or...
Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
k, so here's the problem: this is the code i've written:


#include <iostream>
using namespace std;

int main()

{
int length, width;

cout << "Enter the length: ";
cin >> length;

cout << "Enter the width: ";
cin >> width;

cout << "The area is " << length * width;

return 0;
}

But it's probably not even my code since this works for anything i write.
When i compile and run this file in Dev C++, i can enter the first two digits no problem, but when i hit enter after that, it doesnt display my area, or it might but, the windows closes instantly.

Anyone know how to fix this, is it simply some settings or...
Yes, if you're running it in the console, there is nothing to keep the window open. The answer is being displayed, just not fast enough for you to catch.

The simple solution is to put a cin statement at the end, so it will cin a var, but not matter (no calculations made on it) - this is the "Press Enter key to continue".

Also, code tags make your code more readable, and are appreciated. Thanks!

(Welcome to theScripts!)
Mar 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.