这看起来很简单,但我在这里遗漏了一些东西以使这段代码有效。我要做的是用学生的数字,身份,分数和名字打印25行4列的二维数组内容。
当我用数字初始化数组时,我尝试了类似于这段代码的东西。但是现在我正在从文件中读取数据,我已经碰到了墙,需要帮助。
我尝试在cin对象中使用数组的名称,但我收到如下错误消息:
&#34; assign6.cpp:49:11:错误:不匹配&#39;运算符&gt;&gt;&#39; (操作数类型是&#39; std :: ifstream {aka std :: basic_ifstream}&#39; const int(*)[4]&#39;)&#34; < / p>
所以我把它拿出来,现在代码编译但是我得到了垃圾。有什么建议?很抱歉没有尽快回来。我陷入其他任务中。我做了更改,现在程序正常运行。结果如下。
#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;
}
答案 0 :(得分: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;
答案 1 :(得分:0)
此声明中有错误
cout << "The numbers are"
<< myArray[count][index]
<< getline(inFile,name) << endl;
运营商&gt;&gt;从左到右执行。所以首先输出到cout
cout << "The numbers are"
然后你发送给cout未初始化的myArray值[count] [index]
<< myArray[count][index]
之后
<< getline(inFile,name) << endl;
另外,使用getline时出错。如果您的输入文件只包含空格分隔的int值,那么正确的版本是
inFile >> myArray[count][index];
cout << "The numbers are"
<< myArray[count][index] << endl;