# Thread: Array of Structs Question

1. ## Array of Structs Question

Would the following make sense at all? How could I fix it?

Say I wanted to do this..
Code:
```...

Student studentArray[10];  //declaration of Struct array

struct Student studentArray[10]{
int no;
double average;
};

int x;

for(x = 0; x < 10; x++){
cin >> intInput;
cin >> charInput;
cin >> doubleInput;

Student studentArray[x].no = input1;
Student studentArray[x].average = input3;

intInput = 0;
charInput = "";
doubleInput = 0.0;
}

...```
What is incorrect about this? I want to prompt the user to keep inputting values until they've done it 10 times. Store each set of 3 values in it's own separate struct element of the array.

2. No, it doesnt make sense

First, move the "struct student.." block of code outside of whatever function that is in. If all of this isnt already in a function, ie "int main()", then you certainly need it to be. However, the "struct" type declaration should be outside (and before) any function that you use it in.

Next, "struct Student studentArray[10]" should just be "struct Student". So that you create a new type, called "struct Student", similar how there are types for "int", "char", etc. Later when you use these, you change your line "Student studentArray[10];" to "struct Student studentArray[10];". If you dont want to have to use the word "struct" in that line, you'd have to look at "typedef". See this article (Cprogramming.com FAQ > All about structures), the section talking about "typedef" on how to do that.

Code:
```   cin >> intInput;
cin >> charInput;
cin >> doubleInput```
In the above code, none of those variables are declared. Also, later, you use "charInput" and assign it "" (two double quotes, i.e. an empty string). I assume this variable is a "char", in which case you cant assign it a string and you must use single quotes, like ' '. If it isnt a "char", then change the name of the variable as it is not meaningful and is confusing.

Next, you need to change the lines similar to this "Student studentArray[x]." to "studentArray[x]." And of course "input1", etc, arent declared in the above code, giving more errors.

No, it doesnt make sense

First, move the "struct student.." block of code outside of whatever function that is in. If all of this isnt already in a function, ie "int main()", then you certainly need it to be. However, the "struct" type declaration should be outside (and before) any function that you use it in.

Next, "struct Student studentArray[10]" should just be "struct Student". So that you create a new type, called "struct Student", similar how there are types for "int", "char", etc. Later when you use these, you change your line "Student studentArray[10];" to "struct Student studentArray[10];". If you dont want to have to use the word "struct" in that line, you'd have to look at "typedef". See this article (Cprogramming.com FAQ > All about structures), the section talking about "typedef" on how to do that.

Code:
```   cin >> intInput;
cin >> charInput;
cin >> doubleInput```
In the above code, none of those variables are declared. Also, later, you use "charInput" and assign it "" (two double quotes, i.e. an empty string). I assume this variable is a "char", in which case you cant assign it a string and you must use single quotes, like ' '. If it isnt a "char", then change the name of the variable as it is not meaningful and is confusing.

Next, you need to change the lines similar to this "Student studentArray[x]." to "studentArray[x]." And of course "input1", etc, arent declared in the above code, giving more errors.
The code provided was just snippets that I copied out of my main code. I was just wondering how I could properly create and set values to an Array of structs. I am also clueless to the syntax of struct Array creation. Thanks for the help.

4. Post the simplest compilable code that demonstrate the problem, also describe the problem. Does it fail to compile? What's the error message? Does it crash? Does it give you a result you are not expecting? What did you get and what did you expect?

5. I pretty much want to loop x amount of times and prompt the user for those 3 inputs, store those 3 inputs in a separate struct for x elements of the array. Then be able to regurgitate/edit those values That's basically it.

So something like this:
Code:
```//outside of main

struct Student sArray[]{
int no;
double average;
};

// Sep function (main?)
int input1, x;
char input2;
double input3;

for(x = 0; x < 10; x++){
cin >> input1;
cin >> input2;
cin >> input3;

Student sArray[x].no = input1;
Student stArray[x].average = input3;
}

for(x = 0; x < 10; x++){

cout << sArray[x].no << endl;
cout << sArray[x].average << endl;
}```
But then I don't really understand the point of declaring the members in my struct if all i'm doing is calling them different names. (input1/2/3.. etc)

...If you dont want to have to use the word "struct" in that line, you'd have to look at "typedef"...
This does not hold true for C++. A struct's name can be used directly as a type without any typedef or preceding struct keyword.

Originally Posted by djg1991
Code:
```for(x = 0; x < 10; x++){
cin >> input1;
cin >> input2;
cin >> input3;

Student sArray[x].no = input1;
Student stArray[x].average = input3;
}```
Am I to understand that, somehow, you think that you need to declare names for the members of your struct outside it? This is just wrong.

The names you gave them in the struct ARE the names of the members, and it's how you access them, like you do when printing out:

Code:
```for(x = 0; x < 10; x++){

cout << sArray[x].no << endl;
```for(x = 0; x < 10; x++){