如何在c++;? 数组不是C++中的关键字。

如何在c++;? 数组不是C++中的关键字。,c++,arrays,for-loop,C++,Arrays,For Loop,如果您想声明一个字符串数组,该数组可以保存MAX value=MAX,那么您应该尝试以下方法 字符串名[MAX] 其中“name”是字符串数组的名称,MAX是数组可以存储的字符串总数 您应该使用的整数相同 双级[最大值] 其中grade是整数数组的一个名称。希望这有点帮助。。。(推荐获得C++图书和参考文献) 许多变量在使用前没有声明 例如“name”需要是:string name[] 让我们看看这句话: 字符串数组[name]=MAX 看起来你对C++完全不熟悉。 “String”是数据类型

如果您想声明一个字符串数组,该数组可以保存MAX value=MAX,那么您应该尝试以下方法

字符串名[MAX]

其中“name”是字符串数组的名称,MAX是数组可以存储的字符串总数

您应该使用的整数相同

双级[最大值]


其中grade是整数数组的一个名称。

希望这有点帮助。。。(推荐获得C++图书和参考文献)

许多变量在使用前没有声明

例如“name”需要是:
string name[]

让我们看看这句话:

字符串数组[name]=MAX

<>看起来你对C++完全不熟悉。 “String”是数据类型

“array”是数组的实际名称

“name”是一个未声明的变量,必须是整数才能工作

“MAX”是一个常数。(你在这方面做得很好。)

从您编写它的方式来看,我猜您正在尝试使用字符串的并行数组,并使用600个元素将其双重命名为“name”和“grade”

在C++中,将写:

字符串名称[MAX];//这将创建一个包含600个元素的空字符串数组

等级为:
双级[MAX];//这将创建一个包含600个元素的空双数组

以下代码中的这一部分无法使用,原因如下:

//打开输入文件
infle.open(“indata3.txt”);
//检查文件是否已打开
如果(!infle)
{
名称[x];
cin>>等级[x];
//While(不是文件末尾)
while(填充)
{
//增量x
x++;
//从文件中读取名称和等级,并将其分别指定给名称[x]和等级[x]
cin>>名称[x];
cin>>等级[x];
}

原因有两个:

  • while(不是文件结尾)
    不是真正的代码,您需要:
    while(!infle.eof())

  • cin>>name[x];
    从不访问文件,也不会为元素赋值


  • 祝你好运!谷歌是你的朋友!让人们知道你是个新手,这样他们就不会在stackoverflow荒野中把你撕成碎片!找一个像dev++或CLion这样的好IDE。

    我认为你需要在中找到一本初学者书籍,然后从那里开始。使用一个参考资料,比如找到你需要为
    ifstream
    包含的标题。这可能看起来是不可能的首先,你需要导航,但只要尝试一下,你就会发现它主要是结构良好的,而且还可以搜索。依此类推。@cheers-sandhth.-Alf,给定
    数组[name]=MAX;
    行,我认为问题出在另一个地方。OP几乎解决了
    ifstream
    问题,如果他们在正确的位置将
    cin
    交换为
    infle
    #include <iostream>
    using namespace std;
    
    int main()
    {
    // Declare variable  
        ifstream inFile;
    // Declare constant 
        const int MAX = 600;
    // Declare an array of strings named name that holds up to MAX 
        string array [name] = MAX;
    // Declare an array of whole numbers named grade that holds up to MAX
        double array [grade] = MAX;
    // Declare variables quantity, x, avg, and sum (initialized to zero) that hold whole numbers 
        int sum = 0, avg, x, quantity;
    // Open input file
        inFile.open("indata3.txt");
    // Check if the file was opened
        if (!inFile)
        {
        cout << "File was not found!" << endl;
        return 1;
        }
    // Set x to 0 
        x = 0;
    // Read name and grade from the file and assign them to name[x] and grade[x] respectively
        cin >> name[x];
        cin >> grade[x];
    // While (not-end-of-file) 
        while(inFile)
        {
    //Increment x 
        x++;
    //  Read a name and a grade from the file and assign them to name[x] and grade[x] respectively 
        cin >> name[x];
        cin >> grade[x];
        }
    // Print message 
        cout << "Enter quantity of grades to be processed (0-" << x << "): " << endl;
    // Read a value from the keyboard and assign it to quantity 
        cin >> quantity;
    // For (x = 0 to quantity-1)
        for (x = 0; x <= quantity-1)
        {
    //16. Accumulate grade[x] in sum 
    
        }
    // Assign to avg the value returned by average (sum, quantity)
        avg = sum/quantity;
    // Print "Average grade: ", avg 
        cout << "Average grade: " << avg << endl;
    // Print "Name", "Grade", "   Comment" 
        cout << "Name" << "," << "Grade" << "," << "   Comment"  << endl;
    // For (x = 0 to quantity-1) 
        for (x = 0; x <= quantity-1)
    {
    // Print name[x], grade[x]
        cout << name[x] << ", " << grade[x] << endl;
    // If (grade[x] < avg) 
        if (grade[x] < avg)
        {
    //  Print "   below average"
        cout << "   below average" << endl;
        }
    // Else if (grade[x] > avg) 
        else if (grade[x] > avg)
        {
    //  Print "   above average"
        cout << "   above average" << endl;
        }
    // Else
        else()
        {
    //  Print "   average"
        cout << "   average" << endl;
        }
    }
    // Close the file.
        inFile.close();
    
    return 0;
    }
    
    error C2065: 'name' : undeclared identifier
    error C2075: 'array' : array initialization needs curly braces
    error C2065: 'grade' : undeclared identifier
    error C2371: 'array' : redefinition; different basic types
    error C2440: 'initializing' : cannot convert from 'const int' to 'double [1]'
    error C2228: left of '.open' must have class/struct/union1>    type is 'int'
    error C2143: syntax error : missing ';' before ')'
    error C2059: syntax error : ')'
    error C2143: syntax error : missing ';' before '{'
    error C2228: left of '.close' must have class/struct/union1>    type is 'int'