Cannot get file to display properly.

I have to create a program, that takes a file with employee first name, last name, original salary, opens it, reads it, then does math for a raise and outputs the new data with an updated salary.

SOLVED. THANK YOU.
Last edited on
Oh goodness...

Put your record variables into a struct, then overload the istream and ostream operations for the struct.

EDIT:

An old post I did: http://www.cplusplus.com/forum/beginner/149177/#msg780908
Last edited on
I'm sorry, I'm still kind of learning all this could you please explain further?
I have updated my original post,

Each record has the same variables as you described. Those variables can be put into a data structure, a collection of variables:

1
2
3
4
5
6
struct Record
{
    std:;string first;
    std::string last;
    // Other vars
};


You can then write some code, code that overloads the basic c++ in and out operations ( cin, ifstream etc ) to be able to retrieve and post data, as if it were an int, string etc.

You could then use std::vector to hold a dynamic amount of records ( You can add and take records from the file without having to change the original code, I believe someone on this forum mentions data driven code which opened my eyes up to this.

It may seem confusing as a beginner, I hope my link can help.
Yeah that is far too advanced for me. I appreciate the help you have tried to give me though thank you very much for your time.
Its displaying that way becuase thats how you built a program. Instead of doing it all like that, use a loop. Use a for loop that loops 3 times, since you have 3 persons. Read in all the information, do the math, put it in the file. Do that 3 times.

inFile >> currentSalary1 >> currentSalary2 >> currentSalary3 >> percentIncrease1 >> percentIncrease2 >> percentIncrease3;

Thats not how it works.

If you have a file that looks like this -

Tarik 50 
Jonas 60


And you do - inFile >> currentSalary1 >> currentSalary2

The program wont seek for the "50" and "60" and put them in those variables. Things will be put in the order they are in the file.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
float currentSalary;
double updatedSalary;
double percentIncrease;

string firstName;
string lastName;

for(int i = 0; i < 3; i ++)
{
    // read in first and last name
    // read in salary and percent increase
    // do calculations
    // put them in the file
}
Last edited on
The output is still funky though I have to be doing something wrong.
Post your updated code.
I'm way off. I'm very new to coding so this is a very difficult problem for me to solve.

Would the setw() manipulator help this at all?

SOLVED. THANK YOU.
Last edited on
Ask yourself, why are you only using

1
2
string firstName;
	string lastName;


And then

1
2
3
4
5
6
7
8
9
float currentSalary1;
	float currentSalary2;
	float currentSalary3;
	double updatedSalary1;
	double updatedSalary2;
	double updatedSalary3;
	double percentIncrease1;
	double percentIncrease2;
	double percentIncrease3;


Why dont you use name1, name2, name3, etc like you do with the others? Thats because you dont need it. This is all you need

1
2
3
float currentSalary;
double updatedSalary;
double percentIncrease;


You can read in all info about one person, do the calculations and then put it in the file. Then next loop round it will do the same for next person, and then for the last one.
Here is a code-snippet of how I handled something similar before -

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
ifstream playerFile(fileName);
	if (playerFile.is_open())
	{
		int nrOfPlayers;
		string firstName;
		string lastName;
		int birthYear;
		int numberOfMatches;
		playerFile >> nrOfPlayers;

		for (int i = 0; i < nrOfPlayers; i++)
		{
			playerFile >> firstName >> lastName >> birthYear;
	
			player = Player(firstName, lastName, birthYear);
			playerFile >> numberOfMatches;
			for (int k = 0; k < numberOfMatches; k++)
			{
				string date;
				playerFile >> date;
				player.addMatchDate(date);
			}
			team.add(player); 
		}	
		
	}
Thank you for that, let me try something else and I will post the updated code again hopefully I can get it to work.
Just want to make an update, I have figured it out and it works flawlessly. Thank you for the help again Tarik.
Very happy to hear that! Could you please post your code one last time if someone looks through this thread in the future? Goodluck!
Finally understood the concept of looping after you explained it a bunch of times to me. Really helpful.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream inFile;
	ofstream outFile;

	float currentSalary;
	double updatedSalary;
	double percentIncrease;

	string firstName;
	string lastName;

		inFile.open("f:data.txt");
		outFile.open("f:data.txt");

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

		cout << "<<----PAYROLL SYSTEM---->>" << endl;
		cout << "<<Check output file for updated data>>" << endl;
		for (int i = 0; i < 3; i++)
		{
			inFile >> firstName >> lastName;
			outFile << firstName
				<< " " << lastName;

			inFile >> currentSalary >> percentIncrease;

			updatedSalary = currentSalary * (percentIncrease / 100) + currentSalary;

			outFile << " $" << updatedSalary << endl;

			cout << firstName << ", " << lastName << endl << "Starting salary: $"
				<< fixed << showpoint << setprecision(2) << currentSalary << endl << "Updated salary: $";
			cout << fixed << showpoint << setprecision(2) << updatedSalary << endl << endl;
		}
			inFile.close();
			outFile.close();
		
	return 0;
}
Last edited on
Looks beutiful =)

P.s. Im sure someone most experienced than me could come and make this even better but thats all I got folks :D
Topic archived. No new replies allowed.