| | |
C++ Reading from a text file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
OK, so this program that I am writing basically reads data from a text file then stores the data into an array of objects - one object per line. It then makes the necessary calculations and outputs the results to a file. I managed to get the program to work but I still have problems reading in the data.
Firstly, I am currently taking in the names in the text file into a string and the problem with this is when there is a name with a format such as: "J. p morgan" because i have set aside only two strings, one for the first name and one for the second name, and so when the program attempts to read such a name, the next line is not read in correctly.
Also, for some reason I am not able to read in the full double amount such as 45.55. only 45.5 is stored and not the entire amount although I am using a double variable to store it.
And finally, the last lines in each list is stored twice for some reason in my output file.
So the bottomline is that I need a consistent and efficient way of reading in an entire line from the text file correctly.
I am reading in from two seperate files, one file has the following format:
5 Christine Kim 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F
17 Juan Gonzales 30.00 1 1 M
18 Morris Kramer 8.95 0 0 M
22 Cindy Burke 15.00 1 0 F
24 Esther Bianco 10.25 0 0 F
25 Jim Moore 27.50 3 1 M
32 Paula Cameron 14.50 0 0 F
36 Melvin Ducan 10.25 0 0 M
37 Nina Kamran 30.00 1 1 F
38 Julie Brown 35.00 0 1 F
40 Imelda Buentello 14.50 0 0 F
43 Maria Diaz 15.00 0 0 F
42 J. P. Morgan 12.50 0 0 M
So for example, if the line is "42 J. P. Morgan 12.50 0 0" I need to extract "42", "J. P. Morgan", "12.50", "0", "0"
The other text file has this format:
17 20.0
37 31.0
5 40.0
42 39.5
24 15.0
22 40.0
15 42.0
18 40.0
25 45.0
32 25.0
40 47.5
36 -40.0
16 40.0
38 35.0
43 40.0
33 30.0
This is my entire code:
Firstly, I am currently taking in the names in the text file into a string and the problem with this is when there is a name with a format such as: "J. p morgan" because i have set aside only two strings, one for the first name and one for the second name, and so when the program attempts to read such a name, the next line is not read in correctly.
Also, for some reason I am not able to read in the full double amount such as 45.55. only 45.5 is stored and not the entire amount although I am using a double variable to store it.
And finally, the last lines in each list is stored twice for some reason in my output file.
So the bottomline is that I need a consistent and efficient way of reading in an entire line from the text file correctly.
I am reading in from two seperate files, one file has the following format:
5 Christine Kim 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F
17 Juan Gonzales 30.00 1 1 M
18 Morris Kramer 8.95 0 0 M
22 Cindy Burke 15.00 1 0 F
24 Esther Bianco 10.25 0 0 F
25 Jim Moore 27.50 3 1 M
32 Paula Cameron 14.50 0 0 F
36 Melvin Ducan 10.25 0 0 M
37 Nina Kamran 30.00 1 1 F
38 Julie Brown 35.00 0 1 F
40 Imelda Buentello 14.50 0 0 F
43 Maria Diaz 15.00 0 0 F
42 J. P. Morgan 12.50 0 0 M
So for example, if the line is "42 J. P. Morgan 12.50 0 0" I need to extract "42", "J. P. Morgan", "12.50", "0", "0"
The other text file has this format:
17 20.0
37 31.0
5 40.0
42 39.5
24 15.0
22 40.0
15 42.0
18 40.0
25 45.0
32 25.0
40 47.5
36 -40.0
16 40.0
38 35.0
43 40.0
33 30.0
This is my entire code:
c Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; //Class class EmpInfo { private: int empId; // Used to store employee id char empName[21]; // Used to store employee name double empPayrate; // Used to store employee's pay rate int empDep; // Used to store employee's dependents int empType; // Used to store employee type public: EmpInfo(); EmpInfo(int, char[], double, int, int); void set(int, char[], double, int, int); int getID(); const char *getName(); double getPayrate(); int getDep(); int getType(); }; // End of class EmpInfo::EmpInfo() { empId = 0; strcpy( empName, ""); empPayrate = 0.0; empDep = 0; empType = 0; } EmpInfo::EmpInfo(int Id, char name[], double rate, int dep, int type) { set(Id, name, rate, dep, type); } void EmpInfo::set(int Id, char name[], double rate, int dep, int type) { empId = Id; strcpy(empName, name); empPayrate = rate; empDep = dep; empType = type; } int EmpInfo::getID() { return empId; } const char *EmpInfo::getName() { return empName; } double EmpInfo::getPayrate() { return empPayrate; } int EmpInfo::getDep() { return empDep; } int EmpInfo::getType() { return empType; } const int MAX_EMP = 100; // Max number of employees int search(int, EmpInfo[], int); double printReport(int, EmpInfo[], ofstream&, int); double calcGrosspay(int, EmpInfo[], int); int main() { EmpInfo Emp[MAX_EMP]; // Declare array of structs int numEmp; // employees counter int transNum; // transaction counter ifstream empFile; // Establish input file stream ofstream outputFile; // Establish output file stream string firstName; // Used to extract employee's first name string lastName; // Used to extract employee's first name string tempName; // Store first and last name int id; // Used to store employee id char name[21]; // store name in character array to send to object class double rate; // Used to store employee's pay rate int dep; // Used to store employee's dependents int type; // Used to store employee type int hours; // Number of hours worked by employee //Open employee file empFile.open("master.txt"); //Make sure file exists/opens if (!empFile) { cout << "Can not open employee file \"master.txt\"" << endl; return 1; } numEmp = 0; empFile >> id; empFile >> firstName >> lastName; // Extract first and last name tempName = firstName + " " + lastName; // Concantinate both names tempName.copy(name, 21, 0); // Copy string into array called name empFile >> rate >> dep >> type; empFile.ignore(999, '\n'); while (empFile && numEmp < MAX_EMP) { // Move new employee info into array and increment numEmp count Emp[numEmp].set(id, name, rate, dep, type); numEmp++; // Get next employee info empFile >> id; empFile >> firstName >> lastName; // Extract first and last name tempName = firstName + " " + lastName; // Concantinate both names tempName.copy(name, 21, 0); // Copy string into array called name empFile >> rate >> dep >> type; empFile.ignore(999,'\n'); } empFile.clear(); //close masterfile empFile.close(); // open transaction file empFile.open("trans.txt"); //Make sure file exists/opens transNum = 0; empFile >> id; empFile >> hours; empFile.ignore(999,'\n'); // output invalid transactions outputFile.open("report.txt"); // Print header to file outputFile << setw(10) << "ID:" << setw(20) << "NAME:" << setw(10) << "TAX:" << setw(15) << "INSURANCE:" << setw(15) << "GROSS PAY:" << setw(15) << "NET PAY:" << endl; outputFile << setw(10) << "---" << setw(20) << "-----" << setw(10) << "----" << setw(15) << "----------" << setw(15) << "----------" << setw(15) << "--------" << endl; // Print Error and Control Report Header cout << "INVALID TRANSACTIONS:" << endl; cout << "---------------------" << endl; cout << endl; cout << setw(5) << "ID:" << setw(15) << "HOURS:" << endl; cout << setw(5) << "---" << setw(15) << "------" << endl; int correct; double netPayTotal; double grossPayTotal; while (empFile && transNum < MAX_EMP) { // search to see if id is valid int check = search(id, Emp, MAX_EMP); if(check != -1 && hours >= 0) { netPayTotal += printReport(check, Emp, outputFile, hours); grossPayTotal += calcGrosspay(check, Emp, hours); correct++; } else { cout << setw(5) << id << setw(15) << hours << endl; } empFile >> id; empFile >> hours; empFile.ignore(999,'\n'); transNum++; } cout << endl; cout << endl; // Print Error and Control Report Header cout << "VALID TRANSACTIONS:" << endl; cout << "-------------------" << endl; cout << endl; cout << setw(10) << correct << endl << endl; outputFile << setw(70) << "----------" << setw(15) << "--------" << endl; outputFile << endl; outputFile << setw(55) << "Weekly Total:" << setw(15) << grossPayTotal << setw(15) << netPayTotal << endl; outputFile << setw(55) << "-------------" << endl; // Close outputfile outputFile.close(); // Close inputfile empFile.close(); system("PAUSE"); return 0; } int search(int val, EmpInfo list[], int length) { int index = 0; while(index < length) { if(val == list[index].getID()) return index; index++; } return -1; // id not found } double printReport(int index, EmpInfo Emp[], ofstream &outputFile, int hours) { double tax; // Tax amount for employee double insurance; // Insurance cost for each employee double grossPay; // Pay before insurance and tax double netPay; // Net pay // Calculate grossPay grossPay = calcGrosspay(index, Emp, hours); // Calculate tax tax = grossPay * 0.15; // Calculate insurance insurance = 20.00 * Emp[index].getDep(); // Calculate netpay netPay = ((grossPay - tax) - insurance); // Output to file outputFile << setw(10) << Emp[index].getID() << setw(20) << Emp[index].getName() << setw(10) << setprecision(2) << fixed << tax << setw(15) << setprecision(2) << fixed << insurance << setw(15) << setprecision(2) << fixed << grossPay << setw(15) << setprecision(2) << fixed << netPay << endl; return netPay; }// End of printReport function double calcGrosspay(int index, EmpInfo Emp[], int hours) { double grossPay; // If management employee or if hours <= 40 if (Emp[index].getType() == 1 || hours <= 40) { grossPay = (hours * Emp[index].getPayrate()); } // If non-management employee or hours > 40 else { grossPay = ((40 * Emp[index].getPayrate()) + ((hours - 40) * 1.5 * Emp[index].getPayrate())); } return grossPay; } // End of calcGrosspay functions
Last edited by rockthrower; May 15th, 2007 at 5:10 am.
First of all, please format your code. It's nearly impossible to follow.
You can't use >> when reading a file with evil syntax like you have. Read an entire line using
Generally this is a problem with using
•
•
•
•
Firstly, I am currently taking in the names in the text file into a string and the problem with this is when there is a name with a format such as: "J. p morgan" because i have set aside only two strings, one for the first name and one for the second name, and so when the program attempts to read such a name, the next line is not read in correctly.
getline() and write a function to pull out and convert all the bits and pieces by looking at the line character by character.•
•
•
•
And finally, the last lines in each list is stored twice for some reason in my output file.
.eof() when you loop through the file for reading. This is what happens, feof() is identical to .eof(). The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: May 2007
Posts: 7
Reputation:
Solved Threads: 0
Thanks for the tips!
I am needing to use a pointer to an array of objects in addition to dynamically allocating memory for the array of objects. For some reason, I keep getting error messages:
I am needing to use a pointer to an array of objects in addition to dynamically allocating memory for the array of objects. For some reason, I keep getting error messages:
c Syntax (Toggle Plain Text)
while (empFile && transNum < empCount) { // search to see if id is valid check = search(id, Emp, empCount); [COLOR=red]"cannot convert `EmpInfo*' to `EmpInfo**' for argument `2' [/COLOR] [COLOR=red]to [/COLOR][COLOR=red]`int search(int, EmpInfo**, int)' "[/COLOR] // If ID and hours are both valid if(check != -1 && hours >= 0) { // Calculate netpay total netPayTotal += printReport(check, Emp, outputFile, hours); [COLOR=red]"cannot convert `EmpInfo*' to `EmpInfo**' for argument [/COLOR] [COLOR=red]`2' to `double printReport(int&, EmpInfo**, std::ofstream&, [/COLOR] [COLOR=red]int&)' " [/COLOR] // Calculate grosspay total grossPayTotal += calcGrosspay(check, Emp, hours); [COLOR=red]"cannot convert `EmpInfo*' to `EmpInfo**' for argument [/COLOR] [COLOR=red]`2' to `double calcGrosspay(int&, EmpInfo**, int*)' "[/COLOR] correct++; // Increment number of correct transactions counter } // If ID or hours not valid print out the id and hours else { cout << setw(5) << id << setw(15) << hours << endl; } empFile >> id; // read in ID empFile >> hours; // read in hours empFile.ignore(999,'\n'); // ignore the rest of the line transNum++; // increment counter }
Last edited by rockthrower; May 16th, 2007 at 5:44 am.
Please use the PREVIEW button before SUBMITting your post so you can see yourself if it's readable...
Your errors are telling you to look at the definition of the function and see what you should be passing, but you are not. You are passing a pointer when the function wants a pointer to a pointer
Your errors are telling you to look at the definition of the function and see what you should be passing, but you are not. You are passing a pointer when the function wants a pointer to a pointer
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Reading numbers from a text file (Java)
- Need Help in Reading characters from a text file (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: help pls...
- Next Thread: average array
Views: 14622 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






