Where did I go wrong?


Hello, C++ community!

I am new member to the site and new to the C++ language--or any language for that matter! Anyways, I don't have any prior knowledge to programming as well. I picked up two introductory books and have been messing around. I can make the famous "Hello World!" program (Just to give you an idea what level I'm at).
So now I am making another beginner program that will give you the money earned at work after taxes. The program runs without error but I think its giving me the wrong calculations and I cant find out where I made the error. Here is my code. Any help is highly appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    // Declare Variables
    float payRate = 15.50;
    int hoursWorked;
    float taxRate = 0.14;
    float deduction = payRate * hoursWorked * taxRate;
    float totalEarnings = payRate * hoursWorked - deduction;
    
    cout << "To find out your net pay of the week" << endl;
    cout << "please enter the amount of" << endl;
    cout << "hours worked during the week below" << endl;
    cin >> hoursWorked; 
    cout << totalEarnings << endl;
    
    
    system("PAUSE");
}
1
2
float deduction = payRate * hoursWorked * taxRate;
float totalEarnings = payRate * hoursWorked - deduction;

what is the value of houseWorked....
First you have to take value of houseWorked than you can do some operation on it...like this
1
2
3
4
5
6
7
8
    cout << "To find out your net pay of the week" << endl;
    cout << "please enter the amount of" << endl;
    cout << "hours worked during the week below" << endl;
    cin >> hoursWorked; 
    float deduction = payRate * hoursWorked * taxRate;
    float totalEarnings = payRate * hoursWorked - deduction;
    cout << totalEarnings << endl;
    
Okay, so my error here was just misplacing float deduction and float totalEarnings?

Correct.
Also, there's nothing wrong with declaring them with the other variables. Just don't initialize them with anything that includes 'hoursWorked' since 'hoursWorked' is only assigned a value at line 18.
Last edited on
Topic archived. No new replies allowed.