Reading to file and outputting to file with structs

Hello! I am having an issue with reading from a file and outputting to a file. We are to use a struct called employeeType where we will have string fname, string lname, double current_salary, double pay_increase, and double updated_salary inside of the struct. I get a compiler error saying:

42 23 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] expected primary-expression before '.' token

26 28 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] 'setprecision' was not declared in this scope

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
  #include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

int x;

struct employeeType
{
	string fname;
	string lname;
	double current_salary;
	double pay_increase;
	double updated_salary;
};

int main ()
{

	ifstream inFile;
	ofstream outFile;

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

	//declare variables


	//leaving room for that

	inFile.open("Company.txt");
	outFile.open("Company.out");

if (!inFile)
{
	cout << "Cannot open the input file. " << "The program terminates" << endl;
	return 1; 
}

inFile >> fname > lname;
outFile << "HillTop Chocolate Company" << "\n__________________________" << "\nSpecial Pay Increases for 2015"
<< "\n" << "Employee Name" << "      " << "Increase %" << "      " << "Original Salary" < "      " << "Increase Amount"
<< "      " << "Updated Salary" << "\n" << endl;

system("Pause");
return 0;
}
Are you sure that this is code you compiled to get those errors? The errors don't seem to match the code.

Here are the errors that the online compiler generates(press the gear icon):
1
2
3
4
5
6
 In function 'int main()':
26:28: error: 'setprecision' was not declared in this scope
42:11: error: 'fname' was not declared in this scope
42:19: error: 'lname' was not declared in this scope
44:103: error: invalid operands of types 'const char [7]' and 'const char [16]' to binary 'operator<<'
 
Here is the error's again. I double checked and it is consistent.

Errors:

F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp In function 'int main()':
26 28 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] 'setprecision' was not declared in this scope
42 11 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] 'fname' was not declared in this scope
42 19 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] 'lname' was not declared in this scope
44 103 F:\Class stuff\Programming\Object-Oriented Programming\Homework\Project 2\Project_struct\employee.cpp [Error] invalid operands of types 'const char [7]' and 'const char [16]' to binary 'operator<<'

Here is the error's again. I double checked and it is consistent.

Consistent with what I reported, but not with what you first reported.

What do you not understand about these error messages?

Did you #include all the proper include files?

Did you declare all the variables being used?

Remember start by working on the first error message, fix it, recompile and then on to the next problem.

Last edited on
That was my fault. I forgot to #include <iomanip> and I initialized fname and lname. It works. I'm sorry for all the ignorant questions.
Now I'm not sure why it's not displaying anything except to press any key to exit. Can you advise please?

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


#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int x;

struct employeeType
{
	string fname;
	string lname;
	double current_salary;
	double pay_increase;
	double updated_salary;
};

int main ()
{

	ifstream inFile;
	ofstream outFile;

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


	string fname;
	string lname;


	inFile.open("Company.txt");
	outFile.open("Company.out");

if (!inFile)
{
	cout << "Cannot open the input file. " << "The program terminates" << endl;
	return 1; 
}

inFile >> fname >> lname;
outFile << "HillTop Chocolate Company" << endl;

system("Pause");
return 0;
}

Please post a small sample of your input file.
Here is the entirety of the input file (It's only 5 lines).

Ferguson Sarah 87654.23 3.5
Williams Peter 54332.98 4
King Sandy 47398.75 4.5
Dougherty Mark 96543.65 3.5
Smith George 75555.87 3.5
Nevermind it works. I'm marking this as resolved. I was thinking for some reason it should display to the screen. Stupid me. Thanks for the help!
Now I'm not sure why it's not displaying anything except to press any key to exit.

Where do you expect it to display something? The only place you actually print anything is to the output file. Does the file contain anything?

Okay I had to make a struct for the employee information. I'm having a bit of trouble understanding why the syntax doesn't work. 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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int x;

struct employeeType
{
	string fname;
	string lname;
	double increasePercent;
	double current_salary;
	double pay_increase;
	double updated_salary;
};

int main ()
{

	ifstream inFile;
	ofstream outFile;

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


employeeType emp[5];
emp[0].first = fname;
emp[0].last = lname;
emp[0].increasePercent = 3.5;
emp[0].current = current_salary;
emp[0].increaseAmt = pay_increase;
emp[0].updated_salary = current_salary * increaseAmt;

system{"Pause");
return 0;
}
Last edited on
I ended up figuring out that portion. Now I'm kind of stuck with the updated salary. It won't calculate. Can anyone advise please?

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

using namespace std;

int x;

struct employeeType
{
	string fname;
	string lname;
	double increasePercent;
	double current_salary;
	double pay_increase;
	double updated_salary;
};

int main ()
{

	ifstream inFile;
	ofstream outFile;

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





	inFile.open("Company.txt");
	outFile.open("Company.out");

if (!inFile)
{
	cout << "Cannot open the input file. " << "The program terminates" << endl;
	return 1; 
}
cout << "Processing Data. Please check Company.out for the results.\n" << endl;
outFile << "                            HillTop Chocolate Company \n" << "                            -------------------------\n" << "Employee Name" << "       " << "Increase %" << "       " << "Original Salary" << "       " 
<< "Increase Amount" << "       " << "Updated Salary\n" << "-----------------------------------------------------------------------------------------------\n" << endl;

while(!inFile.eof())
{
	int x = 0;
	double increasePercent = increasePercent / 100;
	employeeType emp[5];
	double updated_salary = emp[0].pay_increase * emp[0].increasePercent;
	inFile >> emp[0].fname >> emp[0].lname >> emp[0].pay_increase >> emp[0].increasePercent;
	outFile << emp[0].fname << " " << emp[0].lname << setw(14) 
	<< emp[0].increasePercent << "%  " << setw(7) << emp[0].pay_increase << setw(9) << emp[0].updated_salary << endl;
	x = x + 1;
}

system("Pause");
return 0;
}
I figured out my issue. The final code is:

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

using namespace std;

int x;

struct employeeType
{
	string fname;
	string lname;
	double increasePercent;
	double current_salary;
	double pay_increase;
	double updated_salary;
};

int main ()
{

	ifstream inFile;
	ofstream outFile;

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





	inFile.open("Company.txt");
	outFile.open("Company.out");

if (!inFile)
{
	cout << "Cannot open the input file. " << "The program terminates" << endl;
	return 1; 
}
cout << "Processing Data. Please check Company.out for the results.\n" << endl;
outFile << "                            HillTop Chocolate Company \n" << "                            -------------------------\n" << "Employee Name" << "       " << "Increase %" << "       " << "Original Salary" << "       " 
<< "Increase Amount" << "       " << "Updated Salary\n" << "-----------------------------------------------------------------------------------------------\n" << endl;

employeeType emp[5];
int i = 0; // i = current chosen employee

while(!inFile.eof())
{
	// Get data from input file into current employee object
	inFile >> emp[i].lname >> emp[i].fname >> emp[i].current_salary >> emp[i].increasePercent;

	emp[i].increasePercent = emp[i].increasePercent / 100;
	emp[i].updated_salary = emp[i].current_salary + (emp[i].increasePercent * emp[i].current_salary);
	emp[i].pay_increase = emp[i].increasePercent * emp[i].current_salary;
	
	outFile << emp[i].lname << ", " << emp[i].fname << setw(10)  << emp[i].increasePercent * 100 << "%  " << setw(10) << emp[i].current_salary << setw(10) << emp[i].pay_increase << setw(10) << emp[i].updated_salary << endl;
	i++; // increase the employee index counter by one. This is kinda like a for loop now.
}

system("Pause");
return 0;
}
Topic archived. No new replies allowed.