简体   繁体   中英

passing a two dimensional array to a function from a data file and output contents

This seems simple enough but I'm missing something here to make this code work. What I'm trying to do is print the contents of the two dimensional array in 25 rows and 4 columns with student's num, id, score, and name.
I experimented with something similar to this code when I initialized the array with numbers. But now that I'm reading the data from a file, I've hit a wall and need help.
I tried using the name of the array in the cin object but I got an error message like this:

"assign6.cpp:49:11: error: no match for 'operator>>' (operand types are 'std::ifstream {aka std::basic_ifstream}' and 'const int (*)[4]')"

So I took that out and now the code compiles but I get garbage. Any suggestions? Sorry about not getting back soon. I got caught up in other assignments. I made changes and now the programs works. Here the results.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

 using namespace std;

 //const
 const int Array_Row = 25;
 const int Array_Col = 4;

 //arrays
 string letterGrade[Array_Row];
 int myScores[Array_Row][Array_Col];
  string names[Array_Row];


 int main()
 {
 int count;
 //int average;

 ifstream  inFile;
 inFile.open("classData.txt");

 int arraySize = 0;

 if(inFile.is_open())
 {
 int counter = 0;
 while(inFile.eof()==false)
 {

 inFile >> myScores[counter][0];
 inFile >> myScores[counter][1];
 inFile >> myScores[counter][2];
inFile >> myScores[counter][3];
getline(inFile, names[counter]); 
 counter++;
 }
 }else

 cout << "Failed";

 for(int counter = 0; counter < Array_Row-2; counter++)
 {

 for(int index = 0; index < Array_Col; index++)
 {
  cout << setw(4) << fixed;
  cout << myScores[counter][index];

 }
 cout << names[counter] << endl;
 }
 inFile.close();

 for(int counter = 0; counter < Array_Row-2; counter++)
 {  

 cout << setprecision(2) << setw(2) << fixed;
 double studentAverage = (myScores[counter][0] + myScores[counter][1] +     myScores[counter][2] + myScores[counter][3])/4.0;
  cout << "Student average is ";
  cout << studentAverage;
  cout << " ......" <<names[counter] << endl;

 if(studentAverage >=90.00)
 letterGrade[counter] = "A";
 else if(studentAverage >=80.00 && studentAverage<=89.99)
 letterGrade[counter] = "B";
 else if(studentAverage >=70.00 && studentAverage<=79.99)
 letterGrade[counter] = "C";
else if(studentAverage >=60.00 && studentAverage<=69.99)
letterGrade[counter] = "D";
else if(studentAverage <59)
letterGrade[counter] = "F";
cout << "Student letter grade is: "<< letterGrade[counter] << endl;

}

double classAverage = 0;
for(int counter = 0; counter < Array_Row-2; counter++)
 {  
      classAverage += (myScores[counter][0] + myScores[counter][1] +     myScores[counter][2] + myScores[counter][3]);
}

     cout << "Class average is : "<< (classAverage/92.0);//calculate class average



    int test1Total = 0;
    for(int index = 0; index <Array_Row-2; index++)
    test1Total += myScores[index][0];
    int test1Average = (test1Total/23.0); //calculates test1 average


       cout <<"\nStudent average for test 1: "  << test1Average <<   setprecision(2) <<fixed;

     int test2Total = 0;
     for(int index = 0; index <Array_Row-2; index++)
     test2Total += myScores[index][1];
     int test2Average = (test2Total/23.0); 
      cout <<"\nStudent average for test 2: "  << test2Average;//calculates test2 average

      int test3Total = 0;
      for(int index = 0; index <Array_Row-2; index++)
      test3Total += myScores[index][2];
       int test3Average = (test3Total/23.0); 
        cout <<"\nStudent average for test 3: "  << test3Average;//calculates  test3 average


      int test4Total = 0;
      for(int index = 0; index <Array_Row-2; index++)
       test4Total += myScores[index][3];
      int test4Average = (test4Total/23.0); 
         cout <<"\nStudent average for test 4: "  << test4Average;//calculates test4 average



   return 0;
}

I can't find the error you've posted, but the problem with the code is the getline(...) function.

This is getline 's prototype: istream& getline (istream& is, string& str);

as you can see it returns an istream which you cannot pass to the << operator. What you can do is:

string str;
getline(inFile, name) >> str;

and then print:

cout << "The numbers are"
     << myArray[count][index]
     << str << endl;

You have an error in this statement

cout << "The numbers are"
     << myArray[count][index]
     << getline(inFile,name) << endl;

Operator >> performed from left to right. So first you output to cout

cout << "The numbers are"

Then you send to cout uninitialized value of myArray[count][index]

<< myArray[count][index]

And after that

<< getline(inFile,name) << endl;

Also, you have an error in using of getline. If your input file contain just space separated int values the right version is

inFile >> myArray[count][index];
cout << "The numbers are"
     << myArray[count][index] << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM