-4

I'm fairly new to programming and mostly self taught and I don't know much about the jargon so I'm not sure about how to search for this and therefore haven't found an answer through just searching. Onto the question

Is there anyway to increment a struct? Like I would in for loops using an int. I'm creating a simple program where I'd input multiple students and want to create multiple instances of structs that increment. Example

struct student{
    string name;
    int id;
}x[i];
i++;

sorta like that. Sorry if I'm a little confusing. Hope someone can help me out.

BluesmokE
  • 9
  • 2

4 Answers4

1

What you want to do is make a collection of student objects.

In C++, the std::vector class is generally the best choice for a collection type, and in your case as well.


How to use a vector

To use an std::vector you must first #include the <vector> header file. Then you can declare an std::vector object with the following syntax:

std::vector<T> name;

where T is the type of elements you want to store in the vector, and name is a name for the collection. In your case, you want the type to be student, and a good name for a collection containing students would be students, for example:

std::vector<student> students;

Adding objects to the vector

Now you can start adding student object to the vector. The push_back() function of the vector class can be used for this, like this:

students.push_back(foo);

where foo is a student variable you have created earlier.


Accessing objects in the vector

Now that you have added a student to the vector, you can access it with the at function:

students.at(0);

The above line will access the first object in the vector. The at function is also really useful if you accidentally try to access an object that's not in the vector, for example, you try to access the second object, which we haven't added yet:

students.at(1);

Because we only have one student stored in the vector so far, trying to access the second student with at will result in an error. Which is great! Now we know we did something wrong, and the error message will probably even tell us what we did wrong.


About vector vs raw array

A raw array like int numbers[10] will not tell us if we accidentally try to access over the end of the array like int x = numbers[10]. There will be no useful error messages to tell us were doing something wrong, the program will probably just silently continue execution and start behaving oddly. Which is what we as programmers don't want. Thus a vector is a superior alternative.

Note that with a vector you also don't have to specify a size, as with raw arrays. vectors will automatically grow as you add new objects to it, so you don't have to worry about adding too many.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • You should put the better way first. – Puppy Feb 09 '15 at 11:12
  • But if he use struct, perhaps he is in C, not C++ – Emrys Myrooin Feb 09 '15 at 11:13
  • @Puppy I could, but I prefer to first answer the actual question, and then suggest improvements. – Emil Laine Feb 09 '15 at 11:14
  • The question does not indicate any preference for abject stupidity. Also, since he tagged C++ and used std::string (apparently) then he is in C++. – Puppy Feb 09 '15 at 11:14
  • 1
    @EmrysMyrooin It's tagged C++. – Emil Laine Feb 09 '15 at 11:15
  • Ho sorry i haven't seen It. – Emrys Myrooin Feb 09 '15 at 11:16
  • @Puppy I get your point, but putting the right way last doesn't mean undervaluing it since I'm explicitly stating it's the better way. The OP both learns the syntax for creating raw arrays of custom objects and also that there's a better way to do what they want. – Emil Laine Feb 09 '15 at 11:23
  • 1
    Additionally `std::vector` even has an `operator[]` to access an element at a specific index (to state the similarity in syntax). – Sambuca Feb 09 '15 at 11:29
  • @zenith: It does explicitly undervalue it. People will not read all the post and they certainly won't understand it. You should not post terrible practices to people who clearly don't understand the very basics and then just disclaimer them. – Puppy Feb 09 '15 at 14:20
  • @Puppy Okay that does make sense. I rewrote the answer accordingly. – Emil Laine Feb 09 '15 at 15:04
-1
struct student{
    student(){}
    student(int i;string n):id(i),name(n){//this is student constructor}
    string name;
    int id;
};
int numberOfStudents=10;
student arrayOfStudents[10];
for (int i=0;i<numberOfStudents;i++){
     arrayOfStudents[i]=student(i,"studentname");
}
octelu
  • 9
  • 1
-1

I'm not exactly sure what you're trying. In case you want to have an array of static size and want to increment over it's element, use this:

const unsigned int SIZE = 10;
struct student{
    string name;
    int id;
}x[SIZE];

and to iterate over it:

for (unsigned int i = 0; i < SIZE; i++) {
    somefunction(student[i]);
}

If you want to have a dynamic amount of elements, you could for example use std::vector

std::vector < student > x;
x.push_back(student("John Doe", x.size() - 1));

Iterating over it works the same way as with arrays.

halfer
  • 19,824
  • 17
  • 99
  • 186
userrr3
  • 483
  • 1
  • 5
  • 12
  • Yes, thank you this is it. Worked perfectly for the static size but I'm not quite sure how to use the dynamic one yet, I'll have to read up. I really didn't know how to explain it but I am glad you understood. – BluesmokE Feb 09 '15 at 11:29
-1

You can use array of struct and traverse them, or use pointers to struct and increment the pointer to traverse.

#include <iostream>

using namespace std;
struct student{
    string name;
    int id;
};
int main()
{
    int noOfStudent;
    cout << "Enter number of student: ";
    cin >> noOfStudent;

    student *x = new student[noOfStudent];

    for ( int i = 0; i < noOfStudent; ++i)
    {
        cout << "Enter Student Name: ";
        cin >> x[i].name;
        cout << "Enter Student ID: ";
        cin >> x[i].id;
    }

    delete[] x;
    return 0;
}
Sridhar Nagarajan
  • 1,085
  • 6
  • 14