【问题标题】:Create object using user input使用用户输入创建对象
【发布时间】:2017-02-07 14:52:12
【问题描述】:

我想使用来自用户的输入创建多个结构对象
例如:
我想接受用户值n 并创建n 数量的对象并将这些对象传递给我初始化变量的函数。

#include <iostream>
#include<string>
#include "stdio.h"

using namespace std;

struct student
{
    int roll_no;
    char name[20];
};

void get_input(student p[],int n1)
{   
    for(int i=1;i<=n1;i++)
    {   
        cout<<"Enter Roll Number ";
        cin>>p[i].roll_no;
        cout<<"\n Enter Name of the student: ";
        cin>>p[i].name;
    }
}

int main()
{

    int n;
    cout<<"How many student details would you want to enter: ";
    cin>>n;
    //Want to create number of object based on input n
    student p[n];
    get_input(student p[],n);

    return 0;
}

【问题讨论】:

  • C++ 中不允许使用可变长度数组。请改用std::vector&lt;student&gt;
  • @Fureeish 那么你可能正在使用一些非标准的编译器扩展。
  • @Fureeish 您的编译器可能支持它作为语言扩展。如果您使用不支持此类扩展的编译器,它将根本无法工作。
  • @Fureeish 那些不是编译器。他们可能都在使用默认支持可变长度数组的 gcc。
  • @Fureeish 如果您将标准设置为-std=c++14 而不是-std=gnu-c++14,您至少应该收到有关使用非标准扩展的警告。

标签: c++ function object struct parameters


【解决方案1】:

你的例子有很多问题。

第一个问题是student p[n];。这不是严格有效的 C++。一些编译器允许它作为扩展。在不知道您使用的是哪个编译器以及带有什么标志的情况下,我会假设这是问题的一部分。这个问题的典型解决方案是使用std::vectorstd::vector 在很多方面都像可变大小的数组一样工作。 std::vector&lt;student&gt; p(n); 将创建一个名为 p 的向量,其中包含 n 默认构造的 student 对象。

下一个问题是get_input(student p[],n);。在传递参数时命名类型是不必要且不正确的。只需写get_input(p,n);。毕竟,当您调用get_input 时,您并没有指定nint。但是,由于p 现在是std::vector,我们需要添加.data() 来获取指向实际数据的指针。它变成get_input(p.data(), n);

最后一个关键问题是循环for (int i = 1; i &lt;= n1; i++)。想象n 是 3。i 将采用的值是 1、2 和 3。但是,数组的索引从 0 开始。如果 n 是 3,您想要访问元素 0、1 和 2。正确的循环是for (int i = 0; i &lt; n1; i++)

这些更改将使您的示例工作,但仍有许多改进之处。

#include <iostream>
#include <vector>

using namespace std;

struct student
{
    int roll_no;
    char name[20];
};

void get_input(student p[], int n1)
{
    for (int i = 0; i < n1; i++)
    {
        cout << "Enter Roll Number ";
        cin >> p[i].roll_no;
        cout << "\n Enter Name of the student: ";
        cin >> p[i].name;
    }
}

int main()
{

    int n;
    cout << "How many student details would you want to enter: ";
    cin >> n;
    //Want to create number of object based on input n
    std::vector<student> p(n);
    get_input(p.data(), n);


    return 0;
}

考虑使用std::string 而不是char name[20]。您不必猜测名称可能有多长,也不会冒因名称较长而导致未定义行为的风险。

struct student
{
    int roll_no;
    std::string name;
};

考虑通过引用传递p,而不是使用指针和大小。

// Declaration / definition
void get_input(std::vector<student> & p)

// Usage
get_input(p);

考虑使用基于范围的 for 循环而不是常规的 for 循环。

void get_input(std::vector<student> & p)
{
    // for each student in p
    for (student & s : p)
    {
        cout << "Enter Roll Number ";
        cin >> s.roll_no;
        cout << "\n Enter Name of the student: ";
        cin >> s.name;
    }
}

【讨论】:

    【解决方案2】:

    使用vectorstudent:以下是一些示例代码,说明如何做到这一点:

    #include <iostream>
    #include <string>
    #include <vector>
    #include "stdio.h"
    
    using namespace std;
    
    struct student
    { int roll_no;
      char name[20];
    };
    
     void get_input(vector<student> & p1, int n1)
    {   
        for (int i=0; i<n1; i++) 
        { 
            student s;
            cout<<"Enter Roll Number: ";
            cin>>s.roll_no;
            cout<<"\n Enter Name of the student: ";
            cin>>s.name;
            p1.push_back(s);
        }
    }
    
    int main()
    {
    
        int n;
        cout<<"How many student details would you want to enter: ";
        cin>>n;
        //Want to create number of object based on input n
        vector<student> p;
        get_input(p, n);
    
    
        return 0;
    }
    

    【讨论】:

    • @Francois 非常感谢它帮助我理清了很多概念。感谢 Rama 让我知道不同的方法
    猜你喜欢
    • 2021-12-21
    • 2015-11-11
    • 2017-10-21
    • 2018-03-12
    • 2015-12-21
    • 1970-01-01
    • 2021-11-18
    • 2017-09-11
    相关资源
    最近更新 更多