使用用户输入创建对象 [英] Create object using user input

查看:48
本文介绍了使用用户输入创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create number of structure objects using input from user
for example:
I want to accept user value n and create n number of objects and pass these objects to a function where I initialize the variables to them.

#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;
}

推荐答案

您的示例存在很多问题.

There are a number of problems with your example.

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

The first problem is student p[n];. This is not strictly valid c++. Some compilers allow it as an extension. Without knowing which compiler you are using, and with what flags, I'll assume this is part of the problem. The typical solution for this problem is to use std::vector. An std::vector works in many ways like an array of variable size. std::vector<student> p(n); will create a vector named p containing n default constructed student objects.

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

The next problem is get_input(student p[],n);. It's unnecessary and incorrect to name the type when passing an argument. Just write get_input(p,n);. After all, you didn't specify that n is int when you called get_input. However, since p is an std::vector now, we need to add .data() to fetch a pointer to the actual data. It becomes get_input(p.data(), n);.

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

The final critical issue is the loop for (int i = 1; i <= n1; i++). Imagine n is 3. The values i will take are 1, 2 and 3. However, arrays are indexed starting at 0. If n is 3, you want to access the elements 0, 1 and 2. The correct loop is for (int i = 0; i < n1; i++).

这些更改将使您的示例可以正常工作,但是仍然可以进行很多改进.

These changes will allow your example to work but there are still many improvements that can be made.

#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] .您不必猜测名称的长度,也不必冒更长的名称带来的不确定行为.

Consider using std::string instead of char name[20]. You won't have to guess how long a name might be, and you don't risk undefined behavior from having longer names.

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

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

Consider passing p by reference, instead of using a pointer and size.

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

// Usage
get_input(p);

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

Consider using a ranged based for loop instead of a regular for loop.

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;
    }
}

这篇关于使用用户输入创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆