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 )
{
int numProgrammers = 0;
int programmer_id;
string name;
int lines;
if(inputFile.is_open())
{
while (!inputFile.eof() && numProgrammers < maxProgrammers)
{
inputFile >> programmers[numProgrammers].programmer_id;
inputFile >> programmers[numProgrammers].name;
inputFile >> programmers[numProgrammers].lines;
numProgrammers ;
}
}
//just a stub!
return 0;
}
//returns the total of the lines field for all programmers
int calcTotalLines( Programmer programmers[], int numProgrammers )
{
int totalLine = 0;
for (int i = 0; i < numProgrammers; i )
{
totalLine = programmers[i].lines;
}
//just a stub!
return totalLine;
}
//returns the average lines coded for all programmers as a float
float calcAverageLines( Programmer programmers[], int numProgrammers )
{
int totalLine;
calcTotalLines(programmers, numProgrammers);
int averageLines = 0;
averageLines = totalLine/numProgrammers;
//just a stub!
return averageLines;
}
//return a string containing info for a particular programmer
string generateProgrammerInfo( Programmer theProgrammer )
{
int programmer_id;
string name;
int lines;
const int SPACING = 5;
stringstream sout;
sout << setw(SPACING) << theProgrammer.programmer_id << theProgrammer.name << theProgrammer.lines << endl;
//just a stub!
return sout.str();
}
//generate report for all programmers
//call calcTotalLines, calcAverageLines, and generateProgrammerInfo functions
void generateProgrammerReport( ostream& output, Programmer programmers[], int numProgrammers )
{
int programmer_id;
string name;
int lines;
Programmer theProgrammer;
calcTotalLines(programmers, numProgrammers);
calcAverageLines(programmers, numProgrammers);
string generateProgrammerInfo( Programmer theProgrammer );
//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 << generateProgrammerInfo(theProgrammer) << endl;
}
output << "\nTotal lines = " << calcTotalLines(programmers, numProgrammers); //this should ouptut the total lines
output << "\nAverage lines = " << calcAverageLines(programmers, numProgrammers); //this should output the average lines
output << endl;
}
The main.cpp file can't be changed but the cpp file can be changed in order to work with the main.cpp file. can't use vectors either.
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.
