Dynamically sized array of struct

Hi there, I am VERY new to C++ and had a question on how to make an array of structures where the size is determined by user input. I've been reading around and find that everyone says to just use vectors instead, but I'd like to learn how to do it using pointers as that is what just starting to learn in my class right now. For example, if I had something like:

1
2
3
4
5
6
 struct data
{
 string name;
 int age;
 int idnumber;
}Student[i];


how would I make it so that i is a number that is prompted to be inputted by the user? If I'm not making sense (and I completely understand if I'm not), this is an idea of what I am trying to do -> http://www.cplusplus.com/forum/beginner/112034/

Thank you!
You could do basically what they had on line 18, except substituting class names etc. where appropriate.

data* data_array = new data[i];

Just make sure you get the value of i from the user first.
Last edited on
Here's a short example of dynamically allocating memory!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main()
{
	int input;

	std::cout << "Array size: ";

	std::cin >> input;

	int *numArray = new int[input];

	for(int i = 0; i < input; i++)
	{
		std::cout << "Please enter value for index " << i << ": ";
		std::cin >> numArray[i];
	}

	for(int i = 0; i < input; i++)
	{
		std::cout << "\nArray value at index " << i << ": " << numArray[i];
	}

	delete[] numArray; //Don't forget to deallocate your memory.

	std::cin.ignore(); //Ignore these two lines, they just keep the console open for me
	std::cin.ignore();

	return 0;
}
Last edited on
Thanks for the quick replies!

@firedraco I copied the example that's shown in the link I provided but as they said, it crashes and I don't know why. :(

edit: nvm got it to work! Using Renthalkx97's example...

edit2: Nowww i'm getting another weird problem. I'm using this code but after I insert the first student's name, the whole program just runs to the end to the code by itself and only shows the first student's age.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

struct data{
int age;
string name;
};

int main()
{
	int input;


	cout << "Number of students: ";

	cin >> input;

	data* student = new data[input];

	cout << "You inputted: " << input << endl;

	for(int i = 0; i < input; i++)
	{
		cout << "\nPlease enter age for student number " << i + 1 << ": ";
		cin >> student[i].age;
		cout << "\nPlease enter name for student number " << i + 1 << ": ";
		getline(cin, student[i].name);
	}

	for(int i = 0; i < input; i++)
	{
		cout << "\nAge of student " << i + 1 << ": " << student[i].age;
		cout << "\nName of student " << i + 1 << ": " << student[i].name;
	}

	delete[] student; //Don't forget to deallocate your memory.


	return 0;
}

Last edited on
The problem is with lines 25/28. You are mixing formatted and unformatted input. Line 25 will leave extra junk in the buffer (in this case a '\n'), which you will need to remove before getline() is called else it will read in nothing.

Try inserting this after line 25:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

This will ignore all characters up to the first newline it encounters, so then when you call getline() it will have to wait for your input.
And it works! Thank you so much.
Topic archived. No new replies allowed.