繁体   English   中英

将二维数组从数据文件传递给函数并输出内容

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

这似乎很简单,但是我在这里缺少一些东西来使此代码起作用。 我想做的是用学生的num,id,score和name在25行4列中打印二维数组的内容。
当我用数字初始化数组时,我尝试了类似于此代码的内容。 但是现在我正在从文件中读取数据,我遇到了麻烦,需要帮助。
我尝试在cin对象中使用数组的名称,但收到了如下错误消息:

“ assign6.cpp:49:11:错误:'operator >>'不匹配(操作数类型为'std :: ifstream {aka std :: basic_ifstream}'和'const int(*)[4]')”

所以我把它取出来,现在代码可以编译了,但是我却得到了垃圾。 有什么建议么? 抱歉不能尽快回来。 我陷入了其他任务。 我进行了更改,现在程序可以运行了。 这里的结果。

#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;
}

我找不到您发布的错误,但是代码的问题是getline(...)函数。

这是getline的原型: istream& getline (istream& is, string& str);

如您所见,它返回一个istream ,您不能将其传递给<<操作符。 您可以做的是:

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

然后打印:

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

您在此声明中有一个错误

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

运算符>>从左到右执行。 所以首先你输出到cout

cout << "The numbers are"

然后,您将myArray [count] [index]的未初始化值发送给cout

<< myArray[count][index]

在那之后

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

另外,在使用getline时出错。 如果您的输入文件仅包含用空格分隔的int值,则正确的版本为

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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