Output Precision

Hello again, I've gotten this basic program to work however I'm having difficulties adjusting my output. Why am I unable to output double decimal places?

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef WAGE_H
#define WAGE_H

class Wage
{
public:
    Wage(int num2,int num3);
    void setValues(int y, float z);
    void setHours(int n);
    void setRate(float n);
    void calculateWage();


private:
    float grossWage;
    int hours;
    float rate;
};

#endif // WAGE_H


.cpp
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
60
61
#include "wage.h"
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::setprecision;

Wage::Wage(int num2, int num3)
{
    setValues(num2,num3);

}

void Wage::setValues(int y, float z)
{
    setHours(y);
    setRate(z);
}

void Wage::setHours(int n)
{
    hours=n;
}

void Wage::setRate(float n)
{
    rate=n;
}

void Wage::calculateWage()
{
   while (hours!=-1)
    {
       int xhours=hours-40;
       float xwage=1.5*xhours;

       if(hours>40)
        {
            grossWage=rate*40+xwage;
            cout<<"Salary is $"<<setprecision(2)<<grossWage<<endl;
            cout<<"Extra hour(s) worked "<<xhours<<endl;
        }

       else if(hours<=40)
        {
            grossWage=rate*hours;
            cout<<"Salary is $"<<setprecision(2)<<grossWage<<endl;

        }



        cout<<"Please enter hour(s) worked(-1 to end): ";
        cin>>hours;
        cout<<"Please enter rate of pay: ";
        cin>>rate;

    }
}


driver
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>
using std::cout;
using std::cin;
using std::endl;

#include "wage.h"

int main()
{
    int num2;
    float num3;
    cout<<"Please enter number of hours worked(-1 to end): ";
    cin>>num2;
    cout<<"Please enter the rate of pay($00.00): ";
    cin>>num3;


    Wage objct(num2,num3);

    objct.calculateWage();

    return 0;
}


I also know about the whole static_cast needed for using different variable types, however at the moment I just wanting to know how to get a decimal output because my setprecision doesn't seem to be effective.

(And sorry if I don't reply immediately currently going to sleep back in 4 hours)
Last edited on
try including cout<<showpoint; before the if statement in .cpp file
Now showing approximately 3 decimal places, any attempt to include a set precision function ends with an output similar to

3.4e2123
Ok my bad, I totally misinterpreted what I was doing, my set precision made it so that only 2 digits were displayed.
replace the code that I gave with this one
cout<<fixed<<showpoint;

I tested it and works fine
Glad to have you know it's working now. Sorry to bother, but due to my insomnia kicking in and having classes at the University all day I wish to ask for a favour regarding my code. Is it because of me using the "set value" functions that my program doesn't terminate immediately upon entering the flag value? and if so is there anyway I can keep the "setValues" or should I switch to a more basic assignment such as:
1
2
3
4
5
6
Wage::Wage(int num2, double num3)
{
    int hours=num2;
    double rate=num3;

}
Last edited on
Create a default constructor in the class.
In the main, create a object of the class and after that call the function
calculateWage.

In the calculateWage, use prime read.

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
void Wage::calculateWage()
{
	 cout<<"Please enter hour(s) worked(-1 to end): ";//prime read
        cin>>hours;
        
   while (hours!=-1)
    {
		cout<<"Please enter rate of pay: ";
        cin>>rate;
       int xhours=hours-40;
       float xwage=1.5*xhours;
	   cout<<fixed<<showpoint;
       if(hours>40)
        {
            grossWage=rate*40+xwage;
            cout<<"Salary is $"<<setprecision(2)<<grossWage<<endl;
            cout<<"Extra hour(s) worked "<<xhours<<endl;
        }

       else if(hours<=40)
        {
            grossWage=rate*hours;
            cout<<"Salary is $"<<setprecision(2)<<grossWage<<endl;

        }



        cout<<"Please enter hour(s) worked(-1 to end): ";
        cin>>hours;
    //    cout<<"Please enter rate of pay: "; //no need of this one here
     //   cin>>rate; // no need

    }
}
Last edited on
Works, and the solution above really is complimentary. I tweaked with my code a lot and it feels good to write your own stuff. Thank you all.
Topic archived. No new replies allowed.