-
February 5th, 2010, 04:57 PM
#1
Array of Structs
I am looking into creating an array of structs, how would I go about doing that?
Code:
#include <iostream>
#include <cstring>
using namespace std;
void etc(struct Student* harry2);
int x[2];
struct Student{
int no;
char grade[14];
};
int main(){
struct Student harry = {234, "ABC"};
etc(&harry);
return 0;
}
void etc(struct Student* harry2){
cout << "enter no." << endl;
cin >> (*harry2).no;
cout << (*harry2).no << " " << (*harry2).grade << endl;
}
I want an array of structs, eachj stuct being a different student with their own student number and course grade within each structure. Thanks.
-
February 5th, 2010, 05:41 PM
#2
Re: Array of Structs
Originally Posted by djgirotto
I want an array of structs, eachj stuct being a different student with their own student number and course grade within each structure. Thanks.
Code:
Student array_of_students[42];
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 5th, 2010, 11:38 PM
#3
Re: Array of Structs
Originally Posted by treuss
Code:
Student array_of_students[42];
How would I access each member?
Code:
Student array_of_students[x].no
?
-
February 6th, 2010, 01:12 AM
#4
Re: Array of Structs
Pretty much, minus the Student at the beginning. Accessing no of the first member would be like this:
Code:
array_of_student[0].no
-
February 6th, 2010, 06:29 AM
#5
Re: Array of Structs
Originally Posted by djgirotto
I want an array of structs,
You could use the new STL array.
http://www.codeguru.com/cpp/cpp/cpp_...le.php/c15257/
-
February 6th, 2010, 08:17 PM
#6
Re: Array of Structs
Say I wanted to do this..
Code:
...
Student studentArray[10]; //declaration of Struct array
struct Student studentArray[10]{
int no;
char grade[3]; //struct members
double average;
};
int x;
for(x = 0; x < 10; x++){
cin >> intInput;
cin >> charInput;
cin >> doubleInput;
Student studentArray[x].no = input1;
Student studentArray[x].grade = input2;
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.
Last edited by djgirotto; February 6th, 2010 at 08:19 PM.
-
February 7th, 2010, 12:59 AM
#7
Re: Array of Structs
The syntax. Your struct is not defined correctly, i.e., it should not have the [10] that is between the struct name and the opening brace.
This:
Code:
Student studentArray[x].no = input1;
Student studentArray[x].grade = input2;
Student studentArray[x].average = input3;
should be:
Code:
studentArray[x].no = input1;
studentArray[x].grade = input2;
studentArray[x].average = input3;
But now it looks like you got mixed up between intInput and input1, etc.
Actually, you could simplify the whole loop to:
Code:
for (int x = 0; x < 10; ++x) {
cin >> studentArray[x].no
>> setw(3) >> studentArray[x].grade
>> studentArray[x].average;
}
-
February 7th, 2010, 03:59 AM
#8
Re: Array of Structs
Originally Posted by laserlight
The syntax. Your struct is not defined correctly, i.e., it should not have the [10] that is between the struct name and the opening brace.
This:
Code:
Student studentArray[x].no = input1;
Student studentArray[x].grade = input2;
Student studentArray[x].average = input3;
should be:
Code:
studentArray[x].no = input1;
studentArray[x].grade = input2;
studentArray[x].average = input3;
But now it looks like you got mixed up between intInput and input1, etc.
Actually, you could simplify the whole loop to:
Code:
for (int x = 0; x < 10; ++x) {
cin >> studentArray[x].no
>> setw(3) >> studentArray[x].grade
>> studentArray[x].average;
}
What does the setw(3) do?
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.
-
February 7th, 2010, 04:30 AM
#9
Re: Array of Structs
Originally Posted by djgirotto
What does the setw(3) do?
setw
-
February 7th, 2010, 07:54 AM
#10
Re: Array of Structs
First define the struct "Student".
Then define your variable "studentArray" as an array of Students.
The other way around is neither logical nor does it work.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|