【问题标题】:How to input string array from the structure in this case?在这种情况下如何从结构中输入字符串数组?
【发布时间】:2020-08-01 08:39:07
【问题描述】:

所以,我是初学者。我有这段代码和一些问题。为了更好地理解,您将需要以下代码:

struct student
{
    double marks;
    char name[50];
}stud[100],t;

int main()
{
    int i,j,n;
    cout<<"Enter the number of students: ";
    cin>>n;
    cout<<"Enter student info as name , marks\n";
    for(i=0;i<n;i++)
    {
        cin>>stud[i].name;
        cin>>stud[i].marks;
    }

问题是,而不是这部分:

struct student
{
    double marks;
    char name[50];
}stud[100],t;

应该有这部分:

struct student
{
    double marks[];
    string name[];
}stud[100],t;

但是我不知道如何将该数据输入到程序中,因为那时 cin >> 不起作用。 任务说,当用户输入“”(ENTER)时,程序应该完成并按顺序显示学生打印。

【问题讨论】:

  • 您需要多少学生分数?你需要另一个循环for
  • 你确定结构应该是这样的吗?另一件事,(假设这是一个学校项目)你可以使用向量吗?
  • 每个学生读一个分,所以双分;没问题(复数没用)
  • 数组不能这样写(除了旧的脏 C-ish 技巧),你需要指定长度。同样string 已经是一个字符串(而char 是一个字符),不需要创建一个数组。
  • 还有一个问题。该程序说,当用户输入“”(ENTER)时,程序应该完成并显示学生按顺序打印。

标签: c++ arrays string input structure


【解决方案1】:

标记和名称数组是动态的,它们被称为灵活数组成员,cpp 不支持它们你可以参考这个链接Are flexible array members valid in C++? 而且它们在 c 中受支持,你最多可以有一个灵活的数组成员和它应该在最后 https://www.geeksforgeeks.org/flexible-array-members-structure-c/

【讨论】:

    【解决方案2】:

    我相信这与您想要的很接近:

    //10 marks by students
    int m = 10;
    
    struct student
    {
        double marks[m];
        string name;
    }stud[100],t;
    
    int main()
    {
        int i,j,n;
        cout<<"Enter the number of students: ";
        cin>>n;
        for(i=0;i<n;i++)
        {
            cout<<"Enter student info as name\n";
            cin>>stud[i].name;
            for(int j=0; j<m; ++j)
            {
                cout<<"Enter student marks "<<j+1<<endl; 
                cin>>stud[i].marks[j];
            }
        }
    }
    

    【讨论】:

    • 谢谢,但任务说当用户输入“”(ENTER)时,程序应该完成并显示学生按顺序打印。所以标记的数量是无限的,用户输入标记直到他输入''。
    【解决方案3】:

    您需要第二个循环。我添加了常量——这是一种很好的做法,因此您的代码中没有“幻数”。我添加了一个显示功能来演示它的工作原理!

    #include <iostream>
    
    using namespace std;
    
    const int MAX_MARKS = 25;
    const int MAX_STR = 50;
    const int MAX_STUDENTS = 100;
    
    struct student
    {
        double marks[MAX_MARKS];
        char name[MAX_STR];
    }stud[MAX_STUDENTS],t;
    
    int main()
    {
        int i,j,n;
        bool complete = false;
        cout<<"Enter the number of students: ";
        cin>>n;
        for(i=0; i < n && i < MAX_STUDENTS; ++i)
        {
            complete = false;
            cout<<"Enter student info as name , marks\n";
            cin>>stud[i].name;
            for (j = 0; j < MAX_MARKS; ++j)
            {
                if (!complete)
                {
                    cout << "Enter mark #" << j+1 << ": ";
                    if (!(cin >> stud[i].marks[j]))
                    {
                        complete = true;
                        stud[i].marks[j] = -1.0;
                        cin.clear();
                        cin.ignore(100, '\n');
                    }
                }
                else
                    stud[i].marks[j] = -1.0; //0.0 is a valid grade so need a different value
            }
        }
    
        //Added a block for displaying the students
        for (i = 0; i < MAX_STUDENTS && i < n; ++i)
        {
            complete = false;
            cout << "Student #" << i+1 << ": " << stud[i].name << endl;
            for (j = 0; j < MAX_MARKS && !complete; ++j)
            {
                if (stud[i].marks[j] == -1.0)
                    complete = true;
                else
                    cout << "\tMark #" << j+1 << ": " << stud[i].marks[j] << endl;
            }
        }
    }
    

    【讨论】:

    • 我对其进行了编辑以满足您的标准——希望这可行!
    • 谢谢,此代码有效。但我们相处得并不好。我们指的是空白,而不是字符。用户可以输入任意数量的学生。当用户输入空格而不是姓名时,即当他或她按下回车键时,应打印先前输入的学生列表,而无需搜索有关其他学生的信息。此外,每个学生只能输入 1 个年级。
    • 我很困惑——那你为什么要问学生人数?另外,正如您在问题中所说,为什么您应该有一个双打数组?
    • 我的错误,对不起。学生人数过剩。该程序的目标是打印从最高到最低的学生排名,输入一次成绩,输入学生姓名,直到用户按下 ENTER。希望我澄清了。
    • 好的,这与您最初陈述的内容大相径庭,需要排序。你以前写过排序吗?
    猜你喜欢
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    相关资源
    最近更新 更多