This is what is in the .txt file-
1 Alvin 300
2 Brenda 350
3 Chris 250
4 Dana 500
5 Eli 450
6 Faye 320
7 Glen 670
8 Hannah 230
9 Ian 420
10 Jade 380
this is the main part of the code that takes the functions from the other cpp file.
int main()
{
Programmer programmers[MAX_PROGRAMMERS];
int numProgrammers = 0;
ifstream inputFile( "programmers.txt" ); // declares file stream and opens for input
if( inputFile.good() )
{
numProgrammers = readProgrammers( inputFile, programmers, MAX_PROGRAMMERS );
//close the input file because we are done with it
inputFile.close();
//report on programmers to console
generateProgrammerReport( cout, programmers, numProgrammers ); // cout passed as an ostream
//report on programmers to a file
ofstream outputFile("ProgrammerReport.txt");
generateProgrammerReport( outputFile, programmers, numProgrammers );
outputFile.close();
}
else
{
cout << "File not opened properly!!\n\n";
}
system("pause");
return 0;
}
This is the cpp file that holds the functions I also have a .h file that declares all the functions. I'm not sure where to start. I have the base plan with all the functions but I'm not sure how to write each of the functions or really just where to start. Any help would be needed.
int readProgrammers( ifstream& inputFile, Programmer programmers[], int maxProgrammers )
{
//just a stub!
return 0;
}
//returns the total of the lines field for all programmers
int calcTotalLines( Programmer programmers[], int numProgrammers )
{
//just a stub!
return 0;
}
//returns the average lines coded for all programmers as a float
float calcAverageLines( Programmer programmers[], int numProgrammers )
{
//just a stub!
return 0.0f;
}
//return a string containing info for a particular programmer
string generateProgrammerInfo( Programmer theProgrammer )
{
//just a stub!
return "";
}
//generate report for all programmers
//call calcTotalLines, calcAverageLines, and generateProgrammerInfo functions
void generateProgrammerReport( ostream& output, Programmer programmers[], int numProgrammers )
{
//just a stub!
output << "These are all the programmers:";
for( int i = 0; i < numProgrammers; i )
{
output << "\nProgrammer info"; //this should output the programmer info for each programmer
}
output << "\nTotal lines = " << 0; //this should ouptut the total lines
output << "\nAverage lines = " << 0.0f; //this should output the average lines
output << endl;
}
CodePudding user response:
Try using fstream:
ifstream file("filename.txt");
string text;
vector<string> lines;
while(getline(file, text)){
lines.push_back(text);
}
And you can do whatever you want with the elements in the vector.
You just have to remember to add this:
#include <fstream>
at the top.
I hope this answers your question!
CodePudding user response:
You have a file of records. Each text line is record.
Let's model a record:
struct Record
{
int person_id;
std::string name;
int value;
};
Let's overload operator>> to make reading a record easier:
struct Record
{
int person_id;
std::string name;
int value;
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
input >> r.person_id;
input >> r.name;
input >> r.value;
input.ignore(10000, '\n'); // Read to end of line for synchronization
return input;
}
The input code could like like this:
std::vector<Record> database;
Record r;
while (my_input_file >> r)
{
database.push_back(r);
}
At this point you have an array of records. You can access fields in a record like:
std::cout << "Name from 5th record: " << database[4].name << "\n";
When you are thinking of parallel arrays, stop. Use a std::vector of struct and overload operator>> to input a record.
