Search code examples
c++fstream

How to read from multiple lines in a file?


I am learning C++ and am having a bit of a hard time with files. This is a little exercise that I am trying to do. The program is meant to read from a file and set it to its proper variables. The text file is as so:

Adara Starr 94
David Starr 91
Sophia Starr 94
Maria Starr 91
Danielle DeFino 94

#include <fstream>
#include <iostream>
using namespace std;

const int MAXNAME = 20;

int main()
{
    ifstream inData;
    inData.open("grades.txt");

    char name[MAXNAME + 1]; // holds student name 
    float average;          // holds student average

    inData.get(name, MAXNAME + 1);

    while (inData)
    {
        inData >> average;
        cout << name << "has an average of " << average << endl;

        //I'm supposed to write something here
    }

    return 0;
}

When I try to run the code, only the first line is being read and displayed and then the program ends. More specifically, the output is

Adara Starr         has an average of 94
Adara Starr         has an average of 0

How do I read the next line from the txt file? I've also done while (inData >> average) in place of the inData condition but it also does the same thing minus the second "Adara Starr has an average of 0"


Solution

  • I hope this helps:

    #include<iostream>
    #include<fstream>
    #include<string>
    using namespace std;
    
    const int MAXNAME = 20;
    
    int main() {
        ifstream inData("input.txt");
        string str = "";
    
        while (!inData.eof()) {//use eof=END OF FILE
            //getline(inData, str); use to take the hole row
    
            /*can use string also
            string firstName;
            string lastName;
            string avg;*/
    
            char firstName[MAXNAME];
            char lastName[MAXNAME];
            float avg;
    
            //you can read from a file like you read from console(cin)
            inData >> firstName >> lastName >> avg;//split the row
    
            cout << firstName << " " << lastName << " " << avg<<"\n";
            
        }
    
        inData.close();
        return 0;
    }
    

    Output: Adara Starr 94

    David Starr 91

    Sophia Starr 94

    Maria Starr 91

    Danielle DeFino 94