Program went too fast

Mar 4, 2012 at 10:11pm
#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
Mar 4, 2012 at 10:15pm
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
Mar 4, 2012 at 10:16pm
Try placing the following code above the return 0;

system("pause");
Mar 4, 2012 at 10:20pm
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
Mar 4, 2012 at 10:21pm
@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"); .
Mar 4, 2012 at 10:26pm
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.
Mar 4, 2012 at 10:32pm
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
Mar 4, 2012 at 10:33pm
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
Mar 4, 2012 at 10:37pm
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
Mar 4, 2012 at 10:44pm
@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.
Mar 4, 2012 at 10:46pm
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
Mar 4, 2012 at 11:04pm
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.
Mar 4, 2012 at 11:25pm
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.
Mar 6, 2012 at 9:34am
now I know, my doctor use "continue without debug"
no wonder, btw where I can find example about compound statement?
+=, -=, *=, /=, and %=
Mar 6, 2012 at 2:05pm
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 Mar 6, 2012 at 2:05pm
Topic archived. No new replies allowed.