Thread: Array of Structs Question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    3

    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;                    
       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.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    Quote Originally Posted by nadroj View Post
    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. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    3
    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;                    
       char grade[3];      //struct members
       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 sArray[x].grade = input2;
       Student stArray[x].average = input3;
    }
    
    for(x = 0; x < 10; x++){
       
       cout << sArray[x].no << endl;
       cout << sArray[x].grade << 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)

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nadroj View Post
    ...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.

    Quote Originally Posted by djg1991 View Post
    Code:
    for(x = 0; x < 10; x++){
       cin >> input1;
       cin >> input2;
       cin >> input3;
    
       Student sArray[x].no = input1;
       Student sArray[x].grade = input2;
       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;
       cout << sArray[x].grade << endl;
       cout << sArray[x].average << endl;
    }
    Likewise, to read into any of the members, you can just do:

    Code:
    for(x = 0; x < 10; x++){
       
       cin >> sArray[x].no;
       cin >> sArray[x].grade;
       cin >> sArray[x].average;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return an array of structs
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-14-2010, 08:31 PM
  2. Question on array
    By spurs01 in forum C Programming
    Replies: 2
    Last Post: 11-25-2009, 07:42 AM
  3. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  4. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM