By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,458 Members | 1,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,458 IT Pros & Developers. It's quick & easy.

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

P: 4
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_HighLowRange();
void Output();
};
void StudentStat::Input()
{

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("EvenClass.txt");
while(!fin.eof())
{
Studobj.Input();
}
fin.close();
return 0;

}
Oct 29 '06 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <iomanip>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. ofstream fout;
  9. ifstream fin;
  10.  
  11. class StudentStat
  12. {
  13. private:
  14.     int Size;
  15.     string Names[20];
  16.     int Scores[20];
  17.     float Avg,
  18.         StDev,
  19.         Median;
  20.     int High_Score,
  21.         Low_Score,
  22.         Range;
  23.     void Storem();
  24.  
  25. public:
  26.     void Input();
  27.     void Calc_Avg();
  28.     void Calc_StDev();
  29.     void Calc_Median();
  30.     void range();
  31.     void Calc_HighLowRange();
  32.     void Output();
  33. };
  34. void StudentStat::Input()
  35. {
  36.  
  37. for(int i=0;i<20;i++)
  38. {
  39.  
  40. fin>>Names[i];
  41. fin>>Scores[i];
  42. cout>>Names[i];              // <---- Here's your problem.  Replace the >> with <<
  43. cout<<Scores[i];              // a.k.a. "cout << Names[i] << " ";
  44. }
  45.  
  46. }
  47. int main()
  48. {
  49. StudentStat Studobj;
  50. fin.open("EvenClass.txt");
  51. while(!fin.eof())
  52. {
  53. Studobj.Input();
  54. }
  55. fin.close();
  56. return 0;
  57.  
  58. }
Found your error - for one of your cout statements, you say cout >> Names[i]; - this should use << instead. Try that.
Oct 29 '06 #2

P: 4
It gives me the error binary '<<' :no operator found which takes right hand operand to type 'std::string'(or 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!
Oct 29 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
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.
Oct 29 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.