Step Step - 1 year ago 101
C++ Question

Create object using user input

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

Answer Source

There are a number of problems with your example.

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.

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);.

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

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

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

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;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download