#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12

    Array of Struct or Class


    What is the proper way to implement an array of a struct or class.
    I have made a struct or class called students.

    Then I declared

    students gradclass[25]

    I am inputting info about students. For some reason it will not let me use single line cin to input into two variables

    for ex cin>>x>>y;

    I intially had it wrong. I initially only had one struct or clas with each internal member being an array. But if I do it like that, it will be hard to sort because I'd have to put lines in the change every member of the stuct or class

    for example it would have been

    gradclass.firstname[i], gradclass.lastname[i], gradclass.gpa[i]. I would have had to go in a change all those.

    If i use an array of struct or class, all I have to change is
    gradclass[i] when I sort.

    When I had it wrong, I was able to input the data the way I wanted to.

    I did cin>>gradclass.first[i]>>gradclass.last[i];
    The first and last names went into thier seperate variables without problem and I was able to pass them through various functions

    Now I'm trying
    cin>>gradclass[i].first>>gradclass[i].last;

    It iniitially seems to work correctly because I put a cout of each right after the input, but it seems that once the loop is over, I seem to loose the first name.

    Code:
     struct students{
      double gpa;
      char ssn[9];
      char first[15];
      char last[15];
    
                   };
    
    
    students gradclass[25];
      for(int i=0;i<=2;i++) {
        cout<<"Enter the next student name: \n";
        cin>>gradclass[i].first>>gradclass[i].last;   
        cout<<"Enter the social security number for ";   
        cin>>gradclass[i].ssn;
        cout<<"Emter the GPA: \n";
        cin>>gradclass[i].gpa;
    for(int i=0;i<=2;i++)
    cout<<gradclass[i].first;
    Since there is only 1 last or first name for each person, I didn't see a reason to make first and last an array of a sting. Am I doing something else wrong? I seem to lose the first name once it goes to the next line of input or output.
    Last edited by Sonic98; February 28th, 2003 at 11:48 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12
    .....
    Last edited by Sonic98; February 28th, 2003 at 11:41 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    I can't duplicate your problem. This works fine:
    Code:
    #include <iostream>
    using namespace std;
    
    struct students{
      double gpa;
      char ssn[9];
      char first[15];
      char last[15];
    
    };
    
    int main()
    {
    	students gradclass[25];
    	
    	for(int i=0;i<2;i++)
    	{
    		cout<<"Enter the next student name: \n";
    		cin>>gradclass[i].first>>gradclass[i].last;
    
    		cout<<"Enter the social security number:\n";   
    		cin>>gradclass[i].ssn;
    
    		cout<<"Enter the GPA: \n";
    		cin>>gradclass[i].gpa;
    	}
    
    	for(int ii=0; ii<2; ii++)
    		cout<<gradclass[ii].first<<endl;
    
    	return 0;
    }
    Last edited by 7stud; February 28th, 2003 at 01:45 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12
    Yeah it works for me now. It must have been sticking a space in there somewhere. Maybe it saw the space as part of the input and cin does stop once it sees space, but it works now.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "I am inputting info about students. For some reason it will not let me use single line cin to input into two variables

    for ex cin>>x>>y;"


    cin skips whitespace, so that couldn't have been your problem, but cin cannot tell that something like:

    TomSmith

    is two pieces of input. You have to separate your data like this:

    Tom Smith
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    Memphis, TN
    Posts
    199
    Rep Power
    12
    I know that. That was the whole point. cin>>first>>last

    They enter thier name like Willie Pep and it stores Willie into first and Pep into last.

    What I had was cout<<"Enter the name "\n"
    then I used the line of input.

    I think I had put a space after the \n, so it would have put a new line then a space. So, there is a possibilty that it might have thought the space place before was part of the input, thus not reading in anything after the space, thus the information I was trying to enter.

IMN logo majestic logo threadwatch logo seochat tools logo