Compiler skipping user prompts? or just my code?

Just a little background. This is my first programming course so I'm a complete newbie. This was an assignment that was due 3 days ago so it doesn't matter anymore, grade-wise. Anyway, the problem that I'm running into is that the compiler skips to the end when I put my initials in //User input > "Initials"

I'm using Microsoft Visual C++ 2010 Express.

Here is the assignment:

Plan and code a program to do the following. You found an exciting summer job for 5 weeks. It pays $15.50 per hour. You will input the number of hours per week that you worked and then compute your total earnings. You must pay taxes of 14%. After paying taxes, you will spend 20% of your money on cloths and 5% on school supplies. After buying clothes and supplies, you will use 25% of the remaining money for savings.

Input
Your 3 initials and the hours for each of the 5 weeks. Use the following numbers of hours for your first test 25, 30, 20, 23, 22.

Calculations
Gross pay is the rate of pay times the sum of all hours you worked. Use CONSTANTS for each of the following rates:

Tax rate 14% of the gross earnings
Clothing 20% of earnings after taxes
School supplies 5% of earnings after taxes
Savings 25% of earnings after taxes and expenses

Output
Output your initials, total hours worked, gross earnings, taxes, net earnings after taxes, clothing expense, supplies expense, amount going to savings and amount left to spend. Output must be aligned to the right as shown with 2 decimals in all numbers. Sample output:

Initials ABC
Total Hours Worked 120.00
Gross Earnings 1860

Taxes paid 260.40
Net Earnings 1599.60

Expenses
Cloths 319.92
School Supplies 79.98

Amount Remaining 1199.70
Savings 299.92

Amount left to spend 899.77

Turn in
Be sure your output file contains user prompts and what was entered by the user. In addition to the results of your program processing. Run with above listed data.

Here is my code

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
58
59
#include <iostream>
#include <iomanip>

using namespace std;

int main()
	
	
{
	
	double Initials, TotalHours, GrossEarn, TaxesPaid;
	double NetEarn, Expenses, ClothAmount, SchoolAmount;
	double AmountRem, Savings, AmountLeft, PayRate;


	const double TAX_RATE = 0.14;
	const double CLOTH_AMT_SPENT = 0.20;
	const double SCHOOL_AMT_SPENT = 0.05;
	const double SAVINGS_PERCENT = 0.25;
	
	// User input
	cout << "Initials: ";
	cin >> Initials;

	cout << "How many hours did you work: ";
	cin >> TotalHours;

	cout << "What is the hourly rate: $";
	cin >> PayRate;

	// Decimal placement
	cout << fixed << showpoint << setprecision(2);
	
	// Calculations
	GrossEarn = TotalHours * PayRate;
	TaxesPaid = GrossEarn * TAX_RATE;
	NetEarn = GrossEarn - TAX_RATE;
	ClothAmount = NetEarn * CLOTH_AMT_SPENT;
	SchoolAmount = NetEarn * SCHOOL_AMT_SPENT;
	AmountRem = NetEarn - SCHOOL_AMT_SPENT - CLOTH_AMT_SPENT;
	Savings = AmountRem * SAVINGS_PERCENT;
	AmountLeft = AmountRem - Savings;

	// Output 
	cout << "Total Hours Worked " << TotalHours << endl;
	cout << "Gross Earnings " << GrossEarn << endl << endl;
	cout << "Taxes Paid " << TaxesPaid << endl;
	cout << "Net Earnings " << NetEarn << endl << endl;
	cout << "Expenses" << endl;
	cout << "Clothes " << ClothAmount << endl;
	cout << "School Supplies " << SchoolAmount << endl << endl;
	cout << "Amount Remaining " << AmountRem << endl;
	cout << "Savings " << Savings << endl << endl;
	cout << "Amount left to spend " << AmountLeft << endl;

	system("pause");


}

Last edited on
Cin is goofy. It does not discard the newline character. Because of this, I suggest the use of the istream overload of getline() and then another function using stringstreams or lexical_cast to convert said input.

Alternatively, you can just discard it yourself and use cin << again.
Last edited on
The professor prohibited using anything we haven't learned in class on our programs. I'm looking through my notes and I don't see any of this stuff. I really appreciate the help but unfortunately I can't do this. I'm going to try it either way. Btw I'm using Microsoft Visual C++ 2010 Express if that matters.
Initials are three characters, not a number.
cin >> Initials; expects a number as input.
If the input is not a number, it goes into a failed state.
Once in a failed state, it does not do anything more unless the failed state is cleared.

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
#include <iostream>
#include <iomanip>

int main()
{
    // Input: Your 3 initials
    char initial_1, initial_2, initial_3 ;
    std::cout << "Initials: ";
    std::cin >> initial_1 >> initial_2 >> initial_3 ;

    // and the hours for each of the 5 weeks.
    double hrs_1, hrs_2, hrs_3, hrs_4, hrs_5 ;
    std::cout << "How many hours did you work (5 times, one for each week): ";
    std::cin >> hrs_1 >> hrs_2 >> hrs_3 >> hrs_4 >> hrs_5 ;

    const double TotalHours = hrs_1 + hrs_2 + hrs_3 + hrs_4 + hrs_5 ;

    double PayRate ;
    std::cout << "What is the hourly rate: $";
    std::cin >> PayRate;

    const double TAX_RATE = 0.14;
    const double CLOTH_AMT_SPENT = 0.20;
    const double SCHOOL_AMT_SPENT = 0.05;
    const double SAVINGS_PERCENT = 0.25;

    // Decimal placement
    std::cout << std::fixed << std::showpoint << std::setprecision(2);

    // Calculations
    const double GrossEarn = TotalHours * PayRate;
    const double TaxesPaid = GrossEarn * TAX_RATE;
    const double NetEarn = GrossEarn - TAX_RATE;
    const double ClothAmount = NetEarn * CLOTH_AMT_SPENT;
    const double SchoolAmount = NetEarn * SCHOOL_AMT_SPENT;
    const double AmountRem = NetEarn - SCHOOL_AMT_SPENT - CLOTH_AMT_SPENT;
    const double Savings = AmountRem * SAVINGS_PERCENT;
    const double AmountLeft = AmountRem - Savings;

    // Output
    std::cout << "Initials " << initial_1 << initial_2 << initial_3 << '\n' ;
    std::cout << "Total Hours Worked " << TotalHours << '\n' ;
    std::cout << "Gross Earnings " << GrossEarn << '\n'  << '\n' ;
    std::cout << "Taxes Paid " << TaxesPaid << '\n' ;
    std::cout << "Net Earnings " << NetEarn << '\n'  << '\n' ;
    std::cout << "Expenses" << '\n' ;
    std::cout << "Clothes " << ClothAmount << '\n' ;
    std::cout << "School Supplies " << SchoolAmount << '\n'  << '\n' ;
    std::cout << "Amount Remaining " << AmountRem << '\n' ;
    std::cout << "Savings " << Savings << '\n'  << '\n' ;
    std::cout << "Amount left to spend " << AmountLeft << '\n' ;
}
Well this makes much more sense. So "char" is only for word inputs? And instead of "std::" I could just replace it with "using namespace std;" at the top?
Last edited on
> So "char" is only for word inputs?

char can be used for character inputs.
1
2
char c ;
std::cin >> c ;

can be used for input of a single (by default, non white-space) character.
Note: '1' is also a character.


> And instead of "std::" I could just replace it with "using namespace std;" at the top?

Yes.

Though using namespace std; at global scope is best avoided.
http://www.parashift.com/c++-faq/using-namespace-std.html
Okay. Thank you so much for the help. I really appreciate it.
Topic archived. No new replies allowed.