Help Needed, Please!!!! Read File And Put Into Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Beginner01
    New Member
    • Sep 2006
    • 4

    Help Needed, Please!!!! Read File And Put Into Arrays

    I need some help reading in a file that has a column with the student's name and then a column with their score.
    I need to input this into 2 seperate arrays. One for the names and one for the scores.
    I have to use a String Array and a Int Array. I can't figure out how to get it into a String array. I keep getting errors.

    Any help would be greatly appreciated!!!


    This is what I have so far:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <iomanip>
    #include <cmath>
    using namespace std;

    ofstream fout;
    ifstream fin;

    class StudentStat
    {
    private:
    int Size;
    string Names[20];
    int Scores[20];
    float Avg,
    StDev,
    Median;
    int High_Score,
    Low_Score,
    Range;
    void Storem();

    public:
    void Input();
    void Calc_Avg();
    void Calc_StDev();
    void Calc_Median();
    void range();
    void Calc_HighLowRan ge();
    void Output();
    };
    void StudentStat::In put()
    {

    for(int i=0;i<20;i++)
    {

    fin>>Names[i];
    fin>>Scores[i];
    cout>>Names[i];
    cout<<Scores[i];
    }

    }
    int main()
    {
    StudentStat Studobj;
    fin.open("EvenC lass.txt");
    while(!fin.eof( ))
    {
    Studobj.Input() ;
    }
    fin.close();
    return 0;

    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by Beginner01
    I need some help reading in a file that has a column with the student's name and then a column with their score.
    I need to input this into 2 seperate arrays. One for the names and one for the scores.
    I have to use a String Array and a Int Array. I can't figure out how to get it into a String array. I keep getting errors.

    Any help would be greatly appreciated!!!


    This is what I have so far:
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    
    ofstream fout;
    ifstream fin;
    
    class StudentStat
    {
    private:
    	int Size;
    	string Names[20];
    	int Scores[20];
    	float Avg,
    		StDev,
    		Median;
    	int High_Score,
    		Low_Score,
    		Range;
    	void Storem();
    
    public:
    	void Input();
    	void Calc_Avg();
    	void Calc_StDev();
    	void Calc_Median();
    	void range();
    	void Calc_HighLowRange();
    	void Output();
    };
    void StudentStat::Input()
    {
    
    for(int i=0;i<20;i++)
    {
    	
    fin>>Names[i];
    fin>>Scores[i];
    cout>>Names[i];              // <---- Here's your problem.  Replace the >> with <<
    cout<<Scores[i];              // a.k.a. "cout << Names[i] << " ";
    }
    
    }
    int main()
    {
    StudentStat Studobj;
    fin.open("EvenClass.txt");
    while(!fin.eof())
    {
    Studobj.Input();
    }
    fin.close();
    return 0;
    
    }
    Found your error - for one of your cout statements, you say cout >> Names[i]; - this should use << instead. Try that.

    Comment

    • Beginner01
      New Member
      • Sep 2006
      • 4

      #3
      It gives me the error binary '<<' :no operator found which takes right hand operand to type 'std::string'(o r there is no acceptable conversion)
      It says the same thing for '>>'.

      Not sure where to go from here.. I have tried to look up sample code like this, but I can't seem to find anything. Without using a vector.

      Thanks for your help!

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Now that's very strange...there is a << operator on cout, and strings are able to be outputted in this way...This isn't likely, but have you tried adding #include <string> to your code? I usually use that header file instead of cstrings.

        Comment

        Working...