What's wrong with my code?

It compiles without error but the 'netPay' result won't display. The console just closes down...

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// assignment1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "string"

using namespace std;


int _tmain(int argc, _TCHAR* argv[])

{

	//Declare variables
string employeeName;
double grossIncome;
double fedTax;
double stateTax;
double socialSecurity;
double medTax;
double penPlan;
double deductions;
double netPay;

cout << fixed << showpoint <<setprecision (2);

cout<< "Enter Employee Name: ";
getline(cin, employeeName);
cout << endl;

cout<< "Enter Employee Gross Income: ";
cin>> grossIncome;
cout <<endl;

fedTax= grossIncome * .15;

stateTax= grossIncome * 3.5/100;

socialSecurity= grossIncome * 5.75/100;

medTax= grossIncome * 2.75/100;

penPlan= grossIncome * 5/100;

deductions= fedTax + stateTax + socialSecurity + medTax + penPlan;

netPay= grossIncome - deductions;

cout << netPay <<endl;



	return 0;
}
You have nothing to tell the console to stay open.

http://cplusplus.com/forum/beginner/1988/
I havent really looked at the code but I'm assuming every thing works ok but the window is closing to fast for you to be able to read the output.

First of all do NOT use as I'm sure it will appear here:

system("pause")

Reasons why are all over the forum.

Use instead:

1
2
cout<<"ENTER to continue.";
cin.get();


just before the return statement.

Happy coding. :)
In such a small, simple project, system("Pause"); will work fine, you don't really need to get into uber complex methods of maintaining the display. When you get into larger projects or something that you may release, then you will want to avoid system(); all together.

The only problem with cin.get(); is that it will read the previous "ENTER"s from previous inputs, so it will still exit before you can see the screen. I read somewhere, I think on the above post I linked to, that you can add cin.sync (); but I can't get it to work with the old Borland Compiler (Instead of firing up an IDE or trying to get another compiler to work from the command line, I just use BCC 5.5 with the Scintilla Text Editor for simplicities sake but with some drawbacks, so with a new compiler it may work). If it still won't work for you, you might want to add cin.get (); //without the "Press ENTER to continue after any input to eat up the ENTERs.

Another solution, run the program from the command line.
If there is any problem with "unread input", all you have to do is:

1
2
cin.clear();
cin.sync();


That's it and no evil system usage.. :)
Yeah, I tried that already, but it still won't work.

Just tried it in Code::Blocks (with the MinGW compiler) and cin.sync(); works without cin.clear ();. Like I said, just an issue with using an old compiler. Of course, Code::Blocks is cool in that it (if run from the "Run" command in the IDE) keeps the console open for you via the "cb_console_runner".

Turns out VC++ doesn't.
It's bets to use cin.clear() if you extract data from the stream before because it will clear the error state if the user entered letters for an integer. Then you use cin.sync(); cin.ignore(); to keep it open - don't use cin.get() because that will only grab one character, and if the user types more than one character you're in trouble for the next input.
Topic archived. No new replies allowed.