extracting data from a file to do calculations

I'm trying to write a program that opens a user-defined file (when prompted), and within that file there are a few lines each consisting of: a name, Social Security Number, birth year, employment start date, monthly salary and job title. Here is my text file:

1
2
3
4
5
6
7
8
9
John Smith 111111111111 1963 2008 4000 Secretary
Tasha Patov 222222222 1973 2010 3000 Marketing
Francie Oldon 3333333333 1983 2008 4000 Secretary
Tuyet Nguyen 4444444444 1993 2012 43750 Sales
Jan Won 55555555 1964 2008 5000 Manager
Julie Nabong 666666666 1989 2010 45000 Manager
Jack Burnside 777777777 1991 2008 2000 Payroll
Larry Zubun 888888888 1980 2010 1500 Manufacturing
Valla Eide 999999999 1948 1980 9000 President




My program is at first supposed to output all the information in that file, and then it is supposed to calculate the bonus of qualifying individuals, the yearly salary of all the individuals, as well as adding the bonus to their salary (if they recieved one). The new information is also supposed to be shown in the program, as well as written out to a new file (called "ReportFile.txt"). Each time I compile it however, I get compilation errors. This 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
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
105
106
 #include <iostream>
 #include <fstream>
 #include <sstream>
 #include <string>
 using namespace std;
 #include <stdlib.h>
 #include <iomanip>


    int main () 

{



     ifstream fin;
     string fileName;
     getline(fin,fileName);
     string firstName, lastName;
     int SSN, birthYear, empStart, mSalary;
     string jobTitle;
     float yBonus, ySalary, totalSalary;

     ySalary *= 12;
     yBonus = static_cast<float>(0.10) *= (mSalary) *= (12);
     totalSalary = (ySalary += yBonus);



// open a file whose name is entered by the user
     cout << "What file do you want to use for input? ";
     getline(cin, fileName);
     fin.open(fileName.c_str), 
     (ios::in|ios::ate, 0);


 //declare some variables for simple calculation
       


     fin>>firstName>>lastName>>SSN>>birthYear>>empStart>>mSalary>>jobTitle;
     while(!fin.eof());
{
          




     if(birthYear <= 1982 || empStart >= 2008);
{

          cout << jobTitle  << " " <<  firstName << " " << lastName << " Salary: " << ySalary;
          cout << " Bonus: " << "0.00" << " Social Security Number: " << SSN << " ";
          cout << "Total yearly salary: " << ySalary << endl;
          
 
    
		
 

	
     
               		     else

                    		cout << jobTitle  << " " <<  firstName << " " << lastName << " Salary: " << ySalary;
                    		cout << " Bonus: " << "0.00" << " Social Security Number: " << SSN << " ";
                    		cout << "Total yearly salary: " << ySalary << endl;}
                    		
                    		

cout << setprecision(0);

fin.close();

{

std::ofstream file;
ofstream fout;
fout.open("ReportFile.txt");
std::ofstream Str("ReportFile.txt,"); 
ofstream myfile(ReportFile.c_str(), ios::out | ios::app);
std::ios_base::app;


          fout << jobTitle  << " " <<  firstName << " " << lastName << " Salary: " << ySalary;
          fout << " Bonus: " << "0.00" << " Social Security Number: " << SSN << " ";
          fout << "Total yearly salary: " << ySalary << endl << endl;




fout.close();







     return 0;


      
	}
          }
	    ;}
Last edited on
54) I don't see the closing brace for the first part of your if statement.
64,68) I don't see matching braces for the else portion of your if statement.
69) I don't see a closing brace for your while statement
41) Your input statement is only ever going to get executed once (i.e. read one record).
75) What's the purpose of the opening brace here?
79-81) You're opening the output file 3 different times.
85-87) You're only going to write output for a single set of input.
104-106) To many closing braces

edit to add:
Organize you program into discrete functions.
16-18) What's the point of these lines? You prompt for the filename at line 32.
24,25) variables are uninitialized.
33) c_str needs to be a function call. i.e. c_str()
49) extraneous ; after the if.
67) Misplaced close brace.
81) Reportfile is not a string
Last edited on
Okay I did the other corrections, but what do you mean by discrete functions?
Your if statement is still messed up.
75) Still has an extraneous brace.
Your input statement is still only going to get executed one.
You still have mutliple instances of the output stream.
ySalary and mSalary are still uninitialized.
You're still only writing data to the output file once.
You're still declaring the output file mutliple times (78,80,81).
You still have extraneous close braces at 105 & 106.

By discrete functions, I meant break your program into subroutines.
For example:
1
2
3
4
5
6
void get_input_record ( )
{}
void process_input_record()
{}
void write_output_record ()
{}

Topic archived. No new replies allowed.