简体   繁体   English

表达式必须具有整数或枚举类型

[英]Expression must have integral or enum type

OK, so I'm trying to do a simple program that reads 2 input files (names & grades), then displays and prints them to an output file. OK,所以我试图做一个简单的程序,读取2个输入文件(名称和等级),然后显示并将它们打印到输出文件中。 So far I have this: 到目前为止,我有这个:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

void ReadNames();
void ReadGrades();

void ReadNames()
{
char names [15][5];

ifstream myfile("names.txt");

if(myfile.is_open())
{
    while(!myfile.eof())
    {
        for (int i = 0; i < 11; i++)
        {
            myfile.get(names[i],15,'\0');
            cout << names[i];
        }
    }
    cout << endl;
}
else cout << "Error loadng file!" << endl;
}

void ReadGrades()
{
char grades [15][5];

ifstream myfile2("grades.txt");

if(myfile2.is_open())
{
    while(!myfile2.eof())
    {
        for (int k = 0; k < 11; k++)
        {
            myfile2.get(grades[k],15,'\0');
            cout << grades[k];
        }
    }
    cout << endl;
}
else cout << "Error loadng file!" << endl;
}

int main()
{

char Name [10];
int  grade [10][10];

ReadNames();
ReadGrades();

for (int i = 0;i < 5; i++)
{
    cout << Name[i];
    for ( int j = 0; j < 5; j++)
    grade [i][j] << " ";
    cout << endl;
}

cout << endl;

system("pause");
return 0;
}

When I try to compile Visual Studio is giving me two errors: 当我尝试编译Visual Studio时,出现两个错误:

illegal, right operand has type 'const char [1]' 非法,右操作数的类型为“ const char [1]”

operator has no effect; 操作员无效; expected operator with side-effect 具有副作用的预期算子

I know it's something simple but I have no idea what the problem is. 我知道这很简单,但我不知道问题是什么。 The error seems to stem from the grade [i][j] << " "; 错误似乎源于grade [i][j] << " "; line. 线。 Any help would be appreciated. 任何帮助,将不胜感激。

The errors are telling you you need somehting like 错误告诉您您需要像

std::cout << grade [i][j] << " ";

grade [i][j] is a char , " " is a const char[1] , and there is no operator<< that operates on such a RHS and LHS combination. grade [i][j]char" "const char[1] ,并且没有operator<<可以在这种RHS和LHS组合上进行operator<<

You are trying to output the value of grade[i][j] but you are not using std::cout . 您正在尝试输出grade[i][j]的值,但未使用std::cout Try like this: 尝试这样:

std::cout << grade [i][j] << " ";

The << is the left shift operator . <<左移运算符 Since it is not defined for char (such as grade[i][j] ), you get the error. 由于未为char定义(例如grade[i][j] ),因此会出现错误。

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

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