Thread: Newbie question - struct & arrays

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Newbie question - struct & arrays

    I am working on a homework assignment (a simple name and grade sorting program) but I am obviously missing something and am not seeing it.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 25
    #define MAXN 10
    
    using namespace std;
    
    int nst; 
    struct NGrade {
           char name[MAX];
           int grade;    
    };
    
    
    int main(){
    
        
    cout << "How many students do you want to process?" << endl;
    cin >> nst;    
    
    struct Ngrade grades[MAXN];
    
    int i;
    for(i=0; i < nst; i++)
    {
        cout << "Enter name for student #" << nst << endl;         
        //cin >> grades[nst].name;         
        
        cout << "Enter grade for student #" << nst << endl;         
        //cin >> grades[nst].grade;     
    }
    
        cout << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The error I get is:

    Code:
    22 D:\Dev-Cpp\grade2\main.cpp elements of array `Ngrade grades[10]' have incomplete type 
    22 D:\Dev-Cpp\grade2\main.cpp storage size of `grades' isn't known
    Ideally I had wanted to set it up so that the array quantity (number of names and grades) in the structure was determined by the user at runtime but I can't seem to get it to take a predetermined number of items.

    Simply put I don't understand why it doesn't like my array declaration.


    Thanks in advance from the frustrated and confused,
    n3m0

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >int nst;

    try to avoid global variables

    >struct Ngrade grades[MAXN];

    That doesnt look right to me. C++ uses classes over structs really.
    Anyway, why would you declare Ngrade again after you have it defined outside main?
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #define MAXN 10
    
    using namespace std;
    
    int nst; 
    struct NGrade {
           char name[MAX];
           int grades;
    };
    
    
    int main(){
    
        
    cout << "How many students do you want to process?" << endl;
    cin >> nst;    
    
    NGrade student;
    
    student.grades;
    student.name;
    
    int i;
    for(i=0; i < nst; i++)
    {
        cout << "Enter name for student #" << nst << endl;         
        cin >> student.grades;
        
        cout << "Enter grade for student #" << nst << endl;         
        cin >> student.name;
    };
    
        cout << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Here i had to change alot but this works, from what i could see you struct syntax was incorrect, you should have a look at how a structure is constructed, don't just take just take my advice tho.
    WhAtHA hell Is GoInG ON

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Thank you. I did have to do some juggling tho'

    Here's what I have now (thanks again ):

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 10
    #define MAXN 10
    
    using namespace std;
    
    int nst; 
    struct NGrade {
           char name[MAX];
           int grades;
    };
    
    
    int main(){
    
        
    cout << "How many students do you want to process?" << endl;
    cin >> nst;    
    
    NGrade student[nst];
    
    student[nst].name;
    student[nst].grades;
    
    
    int i;
    for(i=0; i < nst; i++)
    {
        cout << "Enter name for student #" << i+1 << endl;         
        cin >> student[i].name;
        
        cout << "Enter grade for student #" << i+1 << endl;         
        cin >> student[i].grades;
    };
    
        cout << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Your still using global varialbles. 'nst' can be moved inside the main function to make
    it local.
    Why is there a semi-colon after the for loop block?
    Double Helix STL

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by swgh
    Your still using global varialbles. 'nst' can be moved inside the main function to make
    it local.
    Why is there a semi-colon after the for loop block?
    The semi-colon was my fault, does't it make a difference (I'm just curious) it works without but if ou do put it in what happens?
    WhAtHA hell Is GoInG ON

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    student[nst].name;
    student[nst].grades;
    1. do nothing
    2. access array of students outside its bounds - valide indexes are from 0 till nst-1


    3. You don't check nst value entered by the user. What will happen if he will press 0 or -1 or 100000000 or 1.3 or 'y'? Have you checked your program with all this replies?

    4. why do you need <string.h> ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about 'sizeof' and arrays
    By Sharke in forum C Programming
    Replies: 27
    Last Post: 12-09-2009, 06:30 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 03:59 PM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  5. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM