CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2010
    Posts
    8

    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.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Array of Structs

    Quote Originally Posted by djgirotto View Post
    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    8

    Re: Array of Structs

    Quote Originally Posted by treuss View Post
    Code:
    Student array_of_students[42];
    How would I access each member?

    Code:
    Student array_of_students[x].no
    ?

  4. #4
    Join Date
    Sep 2008
    Posts
    113

    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

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Array of Structs

    Quote Originally Posted by djgirotto View Post
    I want an array of structs,
    You could use the new STL array.

    http://www.codeguru.com/cpp/cpp/cpp_...le.php/c15257/

  6. #6
    Join Date
    Feb 2010
    Posts
    8

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Feb 2010
    Posts
    8

    Re: Array of Structs

    Quote Originally Posted by laserlight View Post
    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.

  9. #9
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Array of Structs

    Quote Originally Posted by djgirotto View Post
    What does the setw(3) do?
    setw

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    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
  •  





Click Here to Expand Forum to Full Width

Featured