Help: code won't run all the way

prog: MS Visual studio 2008

issue:

okay, for some reason, my simple code builds perfect without any errors. however, when i run the code it only runs the first 4 lines of code and then skips the rest and stops there. i seriously don't think its the code either. its completely identical to the one in the book. it doesn't say press any key to continue to finish or anything. This is the simple code too.

#include <iostream>



//using namespace std;

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


int main ()
{
double currentPay = 0.0;
double raiseRate = 0.0;
double raise = 0.0;
double newPay = 0.0;

cout << "Enter current weekly pay: ";
cin >> currentPay;
cout << "Enter base rate: ";
cin >> raiseRate;

raise = currentPay * raiseRate;
newPay = raise + currentPay;

cout << "New pay: " << newPay << endl;

/*cout << "hello world!";*/

return 0;
}

i restarted the projects with console application and empty project. and its set to not set in the properties page.

anybody has any clues to why its not running all the way to cout << "new pay" << newPay << endl;? oh i'm using a vs '05 book, but it shouldn't even matter, right?

appreciate it.

thanks,
strife
*FACEPALM* Please Read: http://www.cplusplus.com/forum/beginner/1988/

The problem is with the console closing on you before you have time to read the output.
you need to type code tags before and after your message. Seasoned programmers may ignore your request for information.


http://www.cplusplus.com/forum/articles/16853/

Last edited on
system("pause"); // this will keep the window open until you close it

so your code will look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>



//using namespace std;

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


int main ()
{
double currentPay = 0.0;
double raiseRate = 0.0; 
double raise = 0.0; 
double newPay = 0.0;

cout << "Enter current weekly pay: ";
cin >> currentPay;
cout << "Enter base rate: ";
cin >> raiseRate;

raise = currentPay * raiseRate;
newPay = raise + currentPay;

cout << "New pay: " << newPay << endl;

/*cout << "hello world!";*/
system("pause"); // this will keep the window open until you close it
return 0;
}
wow! really? i knew it had something to do with pausing the system. so disappointed at myself. This is really helpful.. thanks. this didn't happen to me when i first tried it on the 2005 vs before. serious facepalm. thanks again guys

question. but why does the console close even before reading my last line of code before return? is it because it outputs it right away and then goes straight to return 0?
Last edited on
Yes, that is correct. If you run it from a console window that you opened yourself it won't though. Btw, I'd suggest reading this: http://www.cplusplus.com/forum/articles/11153/
@computergeek,

i tried doing the code that you provided on the link. However, when i tried running it, the code still skips the last line of code which was the raise = currentPay * raiseRate.

What to do?
How do you know it skips it? That line produces nothing on the console.
@moschops,

here is my new code with the new thing right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <limits>


//using namespace std;

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


int main ()
{
	double currentPay = 0.0;
	double raiseRate = 0.0; 
	double raise = 0.0; 
	double newPay = 0.0;

	cout << "Enter current weekly pay: ";
	cin >> currentPay;
	cout << "Enter base rate: ";
	cin >> raiseRate;

	raise = currentPay * raiseRate;
	newPay = raise + currentPay;

	cout << "New pay: " << newPay << endl;

	/*cout << "hello world!";*/

	//system("pause"); //keeps window open till you close it

	cout << "Press ENTER to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );


	return 0;


now this code gets and outputs what needs to be outputted

1
2
3
4
	cout << "Enter current weekly pay: ";
	cin >> currentPay;
	cout << "Enter base rate: ";
	cin >> raiseRate;


now when it gets to the calculation and the last cout to display, the window still closes before it displays it. using the system("pause") statement works just fine. However, i don't want to use that since its not recommended. and i'm trying to use the one suggested by computergeek

1
2
	cout << "Press ENTER to continue...";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );


but this didn't work. i'm not sure what is missing with the new one because it still closes before i get to see the last output.

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

Try this instead:

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


Alternatively, stop messing around trying to trick your IDE into not closing the console it has to open for you and just run your program from the console.
Last edited on
Topic archived. No new replies allowed.