Program went too fast

#include<iostream>
using namespace std;
int main ()
{
double a,b,c,d,e ;

cout<<"please enter 5 decimal numbers"<<endl;
cout<<"1." ;
cin>>a ;
cout<<"2." ;
cin>>b ;
cout<<"3." ;
cin>>c ;
cout<<"4." ;
cin>>d ;
cout<<"5." ;
cin>>e ;
cout<<endl;
cout<<"the numbers that you have entered" <<endl;
cout<<"1."<<a<<endl ;
cout<<"2."<<b<<endl ;
cout<<"3."<<c<<endl ;
cout<<"4."<<d<<endl ;
cout<<"5."<<e<<endl;
cout<<endl;
return 0;
}

I don't know where's my wrong, everything's fine until cin>>e; after that, I just can't read and the program closes too fast
closed account (zb0S216C)
By default, other than input operations, the console was never told to wait. Before return in main( ), type: std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' ); Optionally, if you're on Windows (or even on unix), execute the program from command prompt/terminal.

Wazzak
Try placing the following code above the return 0;

system("pause");
closed account (zb0S216C)
I guess you could in corporate system( ), but don't lean on it, and don't use it within the release build of a project; major security issues, an' all.

Wazzak
@framework @learn2adv
actually Im just a student who took a subject regarding c++ just easy one, so my knowledge is limited to what the teacher teaches and as it's a small part of assignment I try to not use beyond that but it works after I put system("pause"); .
Don't apologize. Reintroducing myself to programming after doing it years ago.

That code was not used from what I remember in the past. That makes two of us who were caught with that problem.

Not sure why it is needed these days other than the fact it stops the execution.
closed account (zb0S216C)
@ctest: For learning purposes, system( )'s fine, but don't too attached to it.

learn2adv wrote:
Not sure why it is needed these days other than the fact it stops the execution.

It's not that system( ) doesn't do the job, it's the fact that it brings security risks, performance penalties, and OS dependency to the table. Like I said, for learning, it's fine. But in production code, it's a risky move.

Wazzak
that makes me wonder, when he teaches in the class, he didn't use it, and everything is fine with <<endl; . I am using the same version 2005 of microsoft express C++ as he did but I upgraded something as it have compatibility error with window 7. what worries me is what will happen if he use the codes in his compiler , afraid will give error, because I need to submit my assignment day after tomorrow
closed account (zb0S216C)
Based on your code which you posted, there should be no compatibility issues. The only issue that may occur is if your tutor has a different OS. system( ) was used in MinGW's compiler suite that came with Dev-C++ many years ago.

Wazzak
@Framework.

I took your advice and removed the system() and replaced with

std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' );

It didn't stop the execution. I haven't advanced to the point in my textbook to understand your code but no luck with it.
No I mean, I try visual c++ express editor 2005, in window 7, the program has compatibility error, then I upgrade package something to make it working in window 7. and he never use system ( ) or others,
just like when he type
#include<iostream>
using namespace std;
int main ()
{
cout<<"Hello world"<<endl;
return 0 ;
}
and debug, the program 'waits' but mine not. he didnt use system ( ) that's why I am asking if that will give other effects something
It's not that system() doesn't do the job. It's that there is no job to be done. (but if it were, it will be terrible doing it, xP)
When a program ends I want for it to end.

It's just a configuration in your IDE. It should launch a wrapper of the program, and is the wrapper the one that expects a key.
system or cin.ignore will not "work" if your program ended prematurely (like by a fatal error)
If you IDE is too stupid, your best option is to open a terminal and execute the program from there.

Things that you could learn if you don't use system("PAUSE");
_ Look, a terminal.
_ input/output redirection
_ pipes
_ command line arguments
_ working directory


what will happen if he use the codes in his compiler , afraid will give error
You should include #include<cstdlib> where system is declared.
Ask your teacher about the issue.
Interesting. I'm assuming you are working on a PC. My professor told us if we want to see the program on a pc we should include system("PAUSE"). He uses that in all the programs we do in class. But I work on a mac at home and he specifically said not to include it if coding in xcode.
now I know, my doctor use "continue without debug"
no wonder, btw where I can find example about compound statement?
+=, -=, *=, /=, and %=
The cin.ignore method is a little convoluted. This is a simpler way of pausing the console.

1
2
3
std::cin.sync();
std::cout << "Press any key to continue...";
std::cin.get();
Last edited on
Topic archived. No new replies allowed.