Hello. I'm running into a problem on an assignment I have for my C++ class. I've done similar things before with reading a file, but normally used a counter to know when to stop reading from a line and go on to the next, or code like this:

ifstream inFile;
inFile.get(ch);
while (ch != '\n' )

However, I cannot figure out how to accomplish this with the program I'm currently making. Here's the data I'm reading from a file:

George Bush 70 77
Barack Obama 70 75 80
Bill Clinton 68 70

The point of the program is to read the names and grades and average them out, but I don't need help with that. I need help with how to have the program read in grades until it reaches the end of the line, and then stopping and going on to the next line. It needs to be able to work with ANY set of data formatted in the same way, with any number of grades. I've tried all I know, but I can't figure out a way to insert a while or if statement using '\n' as the "stop and go to the next line" value. Everything I've tried is leading to garbled output or no output at all.

So, the goal is to read firstname and lastname.

inFile >> firstName >> lastName;

Then read in a grade, perform the mathematical calculations on it, and continue on until there are no more grades left on the line. Again, this needs to work with any properly formatted data. What I provided is a sample. So how can I make it stop reading grades and go to the next line? If you could provide me with some direction or code sample on how I can accomplish this I'd greatly appreciate it.

George Bush 70 77
Barack Obama 70 75 80
Bill Clinton 68 70

Does the general format goes like so :

FirstName LastName score1 score2 ... scoreN

or are there only score1 and score2 for all names?

Either way you should do something like so :

struct President{
 string firstName;
 string lastName;
 President(string f = "", string l = ""): firstName(f), lastName(l){}
};

struct PresidentScoreKeeper{
 President pres;
 std::vector<int> scores;
 PresidentScoreKeeper(){}
 PresidentScoreKeeper(const President& p, const std::vector<int> s = ""): pres(p), scores(s){}
};
class PresidentScoreList{
private:
 std::vector<PresidentScoreKeeper> presidentList;
public:
 void add(const PresidentScoreKeeper& p){ presidentList.push_back(p);}
 //more functionalities
};

std::istream& operator>>(std::istream& inputStream, PresidentScoreList& president){
 string line;
 getline(inputStream,line);//KEY IDEA
 President currPresident= prasePresidentName(line);
 std::vector<int> currentPresidentScore= prasePresidentScores(line);
 president.add( PresidentScoreKeeper(currPresident,currentPresidentScore) );

 return inputStream;
}

The key thing is to use getline(inputStream,line). That is, you need to read a line by line for each line in the file instead of character by character.

Do some research on the getline() function. It is in the iostream library. It has an overloaded form that accepts a third argument which is the delimiting character.

That code is far above the level of advancement we've done in the class. It's entry-level C++, CSC 101. We just recently learned for statements and predefined functions, to give you an idea. Vector, struct, almost half of that code, we've never used before.

Anyway, I was trying to figure out how to use getline() to accomplish this, but I'm not really sure how to go about it. I don't think that's how he intends for us to work the problem.

What I need to happen is something like this:

ifstream inFile;
    inFile.open("prog8Data.txt");
    
    cout << fixed << showpoint << setprecision(1);
    gradesum = 0;
    topavg = -1;

    do
    {
    infile >> firstName >> lastName >> grade;
    while ( grade != '\n' ) // I know this part doesn't work, but this is what I need to figure out
          {
          gradesum += grade;
          gradecntr++;
          if (grade > topavg)
             {
             topavg = grade;
             topfirstName = firstName;
             toplastName = lastName;
             }
          inFile >> grade; 
          }
    avg = gradesum / gradecntr;
    cout << lastName << "," << firstName << " " << avg << endl; // Output e.g. Bush,George ##
    }
    while( !inFile.eof() ); 
    
    cout << toplastName << "," << topfirstName << " had the highest average: "; 
    cout << " " << topavg << endl << endl; // Outputs the president with the highest average

    inFile.close();

The program must use a do...while and for the most part, the program is fine. Line 11 is the problem. I know it's wrong, but I had to put something there to give you an idea of what I'm trying to accomplish. I need those operations to run until I hit the end of the line.

while ( grade != '\n' )

Isn't grade an int or double?? This will never work unless its a character array or string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.