1

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;
}
Rama
  • 3,222
  • 2
  • 11
  • 26
Step
  • 307
  • 3
  • 11
  • 5
    Variable length arrays are not allowed in C++. Use a `std::vector` instead. – nwp Feb 07 '17 at 14:56
  • @nwp what do you mean? `cin >> n; student p[n];` works perfectly fine. To OP: well, what is the question? I see a lot of typos/basic errors but can't really post an answer to non-existing question – Fureeish Feb 07 '17 at 14:57
  • 3
    @Fureeish Then you are probably using some non-standart compiler extension. – Yuriy Ivaskevych Feb 07 '17 at 15:00
  • 3
    @Fureeish your compiler may support it as a language extension. It won't work at all if you use a compiler that does not support such extension. – eerorika Feb 07 '17 at 15:01
  • I'm actually surprised. I'm not experienced C++ user (still student) but all the compilers (default Code::Blocks, newest MinGWs..) I've been using allowed me to do that – Fureeish Feb 07 '17 at 15:04
  • 2
    @Fureeish Those are not compilers. They may all be using gcc which supports variable length arrays by default. – François Andrieux Feb 07 '17 at 15:05
  • 1
    @Fureeish If you set the standard to `-std=c++14` instead of `-std=gnu-c++14` you should at least get warnings about using a non-standard extension. – nwp Feb 07 '17 at 15:07
  • What is your question? Does your code not work? (Incidentally, you should add `#include ` and change the declaration of `name ` to `std::string name;`. That way you won't run in problems if 'George Alexander Louis Windsor' enrols - actually I would overrun 20 characters if I used my middle name.) – Martin Bonner supports Monica Feb 07 '17 at 15:08
  • The name of the array in `main` is "p", not "student p[]". – molbdnilo Feb 07 '17 at 15:11
  • @FrançoisAndrieux, seems I lack basic knowledge. Thank you for that information! – Fureeish Feb 07 '17 at 15:11
  • Also, a collection with k elements is indexed from 0 to k-1, not 1 to k. You should get one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 07 '17 at 15:12
  • student p[n] is array of objects here I am trying to create n objects based on user input – Step Feb 07 '17 at 15:14
  • @nwp what is the syntax for using vectors and creating objects i have used this-- error: ‘p’ was not declared in this scope std::vector p(n); – Step Feb 07 '17 at 15:19
  • 1
    @Step You probably did something silly like forgot `#include `. The answers show how to use it. – nwp Feb 07 '17 at 15:22

2 Answers2

1

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;
    }
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
0

Use a vector of student: Here is some example code of how you can do it:

#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;
}
Rama
  • 3,222
  • 2
  • 11
  • 26
  • @Francois thanks a lot it helped me clear a lot of concepts. Thanks to Rama for letting me know different approach – Step Feb 07 '17 at 15:29