need help with formatting and math error

Sorted by name
ID Name Salary
------------------------------------
, $
2000 Adams, John $15000
1717 Bush, George $80000
1515 Carter, Jimmy $78000
1212 Jefferson, Thomas $34000
1313 Lincoln, Abraham $45000
1000 Washington, George $10000


Sorted by salary
ID Name Salary
------------------------------------
, $
1000 Washington, George $10000
2000 Adams, John $15000
1212 Jefferson, Thomas $34000
1313 Lincoln, Abraham $45000
1515 Carter, Jimmy $78000
1717 Bush, George $80000

Total Payroll $2.60867e-321

how do i get the numbers to allign ive tried right and left and setw() but that doesnt work also for some reason a comma and a $ get printed at the beginning of each report and my total payroll is fucked up can someone help me figure this out





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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

class Presidents {
public:
	string firstname;
	string lastname;
	string salary;
	string id;
		
	Presidents (const string &firstname, 
		    const string &lastname, 
		    const string &salary, 
		    const string &id) {
		    this->firstname = firstname;
		    this->lastname = lastname;
		    this->salary = salary;
		    this->id = id;
	}
}; 

int get_file (ifstream& in, ofstream& out, string filename, string output); //this function opens the input file and output file and checks to see if they are open
void get_line (ifstream& in, ofstream& out); //this function brings the input file into a string and then changes all lowercase letters to uppercasse and leaves uppercase letters alone and changes digits to a * symbol then outputs its a$
//void name (ifstream& in, ofstream& out);
//void salary (ifstream& in, ofstream& out);
 
int main () {
	
        ifstream in; //instream
        ofstream out; //outstream
        string filename, output; //input and output file
        get_file (in, out, filename, output);
        get_line (in, out);
//        name (in, out);
//        salary (in, out);
}
int get_file (ifstream& in, ofstream& out, string filename, string output) {
        cout << "enter the file you would like to open" << endl;
        cin >> filename; //takes in the name of the file your opening
        in.open (filename.c_str()); //opens the file
        if (in.fail()) { //checks to see if the file is open
               cout << "that is not a file" << endl;
               return 0;
        }
        cout << "Input the name of the output file" << endl;
        cin >> output;
        out.open (output.c_str());
        if (in.fail()) {
                cout << "that is not a file" << endl;
                return 0;
        }

	return 0;
}
void get_line (ifstream& in, ofstream& out) {
	std::vector <Presidents> sort;
        string line;
	double total;
	out << "Sorted by name" << endl;
	out << "ID      Name                Salary" << endl;
	out << "------------------------------------" << endl;
        while (getline (in, line)){ // using getline to read in a single line at a time
		string id, firstname, lastname, salary;
		istringstream iss (line);
		iss >> id >> firstname >> lastname >> salary;
		Presidents person (firstname, lastname, salary, id);
		sort.push_back (person);
	}
	std::sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });
        for (Presidents i : sort) {
		out << i.id << "    " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
	}
	out << endl << endl;
	out << "Sorted by salary" << endl;
	out << "ID      Name                Salary" << endl;
	out << "------------------------------------" << endl;
	while (getline (in, line)){ // using getline to read in a single line at a time
		string id, firstname, lastname, salary;
		int money = 0;
		istringstream iss (line);
		iss >> id >> firstname >> lastname >> salary >> total;
		if ( ! (istringstream(salary) >> money)) money = 0;
		Presidents person (firstname, lastname, salary, id);
		sort.push_back (person);
		total = money + total;
	}
	std::sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.salary < b.salary; });
        for (Presidents i : sort) {
		out << i.id << "    " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
	}
	
	out << endl << "Total Payroll $" << total << endl;
        in.close (); //closes the input file
        out.close (); //closes the output file
}

Last edited on
also for some reason a comma and a $ get printed at the beginning of each report

Please post a sample of your input file.

my total payroll is fucked up can someone help me figure this out

Where are you initializing total to some initial value?

how do i get the numbers to allign ive tried right and left and setw() but that doesnt work


Show your code that shows what you've tried.




closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/212940/
my input file is
1000 George Washington 10000
2000 John Adams 15000
1212 Thomas Jefferson 34000
1313 Abraham Lincoln 45000
1515 Jimmy Carter 78000
1717 George Bush 80000
for total when i declared it i have = 0 now like double total = 0; and the output is total payroll = 0 so thats still messed up and i dont know why and how many attempts do you want me to put
You need to enclose your sample input file into code tags to preserve the actual file format.

Why are you trying to read the file twice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
       while (getline (in, line)){ // using getline to read in a single line at a time
		string id, firstname, lastname, salary;
		istringstream iss (line);
		iss >> id >> firstname >> lastname >> salary;
		Presidents person (firstname, lastname, salary, id);
		sort.push_back (person);
	}
	std::sort (sort.begin(), sort.end(), [] (const Presidents &a, const Presidents &b){return a.lastname < b.lastname; });

...
//////// You've already read the complete file above so why try (and fail) to read the file again? ///////
	while (getline (in, line)){ // using getline to read in a single line at a time


And you will need to make sure total is initialized before you try to use it.

By the way having a variable that has the same name as a standard function is really not a good idea. I would rename your "sort" variable to a better more descriptive name.

because i need to read it twice because i sort it in different ways if you look at the output unless i only need to read it in once also the input file is in its actual format its exactly like that
1
2
3
4
5
6
1000 George Washington 10000
2000 John Adams 15000
1212 Thomas Jefferson 34000
1313 Abraham Lincoln 45000
1515 Jimmy Carter 78000
1717 George Bush 80000

and i already initialized total by doing this double total = 0; and the output file just says the total payroll is 0 also thanks for the tip about changing sort
Last edited on
because i need to read it twice because i sort it in different ways

Why do you need to read the file twice just to sort the vector?

Answer, you don't and you're not really reading the file twice, which is why money is staying at zero. When you finish with your first loop the stream is in an error state (eof was triggered) and when a stream is in an error stream no further operations on the stream will occur until the error is cleared, then it would be possible to re-position the file pointer to the beginning of the file and then re-read the file.

However re-reading the file is not necessary in this case, just do the "money" calculations in the first loop.

Something 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
33
34
35
36
37
38
39
...
    while(getline(in, line))    // using getline to read in a single line at a time
    {
        string id, firstname, lastname, salary;
        istringstream iss(line);
        iss >> id >> firstname >> lastname >> salary;

        sort.push_back({firstname, lastname, salary, id});  // Using initialization semantics.

        double money = 0.0;
        istringstream(salary) >> money;

        total += money;
    }

    std::sort(sort.begin(), sort.end(), [](const Presidents & a, const Presidents & b)
    {
        return a.lastname < b.lastname;
    });

    for(Presidents i : sort)
    {
        out << i.id << "    " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
    }

    out << endl << endl;
    out << "Sorted by salary" << endl;
    out << "ID      Name                Salary" << endl;
    out << "------------------------------------" << endl;

    std::sort(sort.begin(), sort.end(), [](const Presidents & a, const Presidents & b)
    {
        return a.salary < b.salary;
    });

    for(Presidents i : sort)
    {
        out << i.id << "    " << i.lastname << ", " << i.firstname << " " << setw(12) << right << "$" << left << i.salary << endl;
    }


thank you very much for this it helps me learn
Last edited on
Topic archived. No new replies allowed.