| | |
Reading/writing .txt files into an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I am writing a program and I have a .txt file that has a some information about people. I would like to write that information into an array and then display that information about the person.
The problem is that I don't know how many names will be in the .txt file, or how to write that information to the file.
Here is what I have so far.
and here is my hourlyEmployee class.
I don't really know how to put the .txt information into the array so if anyone could give me some idea's or any help at all that would be great.
Thanks
The problem is that I don't know how many names will be in the .txt file, or how to write that information to the file.
Here is what I have so far.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include "StaffEmployee.h" #include "HourlyEmployee.h" #include <iomanip> #include <fstream> using namespace std; void upload() { ifstream infile; infile.open ("hourly.txt"); while (!infile.eof()) { } } int main() { const int size = 20; HourlyEmployee a[size]; HourlyEmployee fellow; HourlyEmployee person(10, 55, "Electronics", "Alan", "Good", "E1234") ; fellow.display(); person.display(); return 0; }
and here is my hourlyEmployee class.
c++ Syntax (Toggle Plain Text)
#include "Employee.h" #include <iostream> #include <string> using namespace std; #ifndef HOURLYEMPLOYEE_H #define HOURLYEMPLOYEE_H class HourlyEmployee:public Employee { private: double hourlyWage; double hoursWorked; string deptName; public: HourlyEmployee() { hourlyWage = 0; hoursWorked = 0; deptName = "unknown"; } HourlyEmployee(double hWage, double hWorked, string dName, string first, string last, string ID) : Employee(first, last, ID) { hourlyWage = hWage; hoursWorked = hWorked; deptName = dName; } void set_hourlyWage(double hWage) { hourlyWage = hWage; } void set_hoursWorked(double hWorked) { hoursWorked = hWorked; } void set_deptName(string dName) { deptName = dName; } double get_hourlyWage() { return hourlyWage; } double get_hoursWorked() { return hoursWorked; } string get_deptName() { return deptName; } void compute_salary() { double salary = 0; salary = hoursWorked * hourlyWage; cout << "2 Week Salary: $" << salary; cout << endl; } void display() { Employee::display(); double salary = 0; salary = hoursWorked * hourlyWage; cout << "2 Week Salary: $" << salary; cout << endl; cout << endl; } }; #endif
I don't really know how to put the .txt information into the array so if anyone could give me some idea's or any help at all that would be great.
Thanks
Last edited by Ancient Dragon; Dec 2nd, 2007 at 8:48 pm. Reason: added line numbers to existing code tags -- this is not a rules violation but just a convenience.
That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
C++ Syntax (Toggle Plain Text)
string line; while(getline(infile, line) ) { // now split this string into individual parts for the structure }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
That while statement on line 13 is incorrect because eof() doesn't work like that. Instead, code it something like this
C++ Syntax (Toggle Plain Text)
string line; while(getline(infile, line) ) { // now split this string into individual parts for the structure }
Yes. But it depends on lots of things -- format of the file, what the array looks like, what delimiters are in the string. Details help make decisions.
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
•
•
•
•
Yes. But it depends on lots of things -- format of the file, what the array looks like, what delimiters are in the string. Details help make decisions.
the .txt file is...
Joe Cruise E1111
Bill Bones E2222
Sue Smith E3333
Tom Turpin E4444
Ron Stewart E5555
Mindy Sweet E7777
and the class file employee has string firstName, string lastName, string employeeID.
i want to use an array in my int main to write those names and ID's to the class....
is that possible and if so how would I even start to do it?
The input line can be split using stringstream class
C++ Syntax (Toggle Plain Text)
#include <sstream> ... ... stringstream stream(line); stream >> firstName >> lastName >> employeeID;
Last edited by Ancient Dragon; Dec 2nd, 2007 at 10:30 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- fstream Tutorial (C++)
- C++ Reading from a text file (C++)
- Saving to a file from an array in C ? (C)
- only a couple of hours to go... (C++)
- Parse XML from ASP!!! (ASP)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Just a lil fun
- Next Thread: C++ for loops
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






