C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 28th, 2003, 11:26 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 195 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 44 sec
Reputation Power: 6
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
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.

Reply With Quote
  #2  
Old February 28th, 2003, 11:36 AM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 195 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 44 sec
Reputation Power: 6
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
.....

Last edited by Sonic98 : February 28th, 2003 at 11:41 AM.

Reply With Quote
  #3  
Old February 28th, 2003, 01:37 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #4  
Old February 28th, 2003, 03:23 PM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 195 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 44 sec
Reputation Power: 6
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
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.

Reply With Quote
  #5  
Old February 28th, 2003, 09:29 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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

Reply With Quote
  #6  
Old February 28th, 2003, 11:25 PM
Sonic98 Sonic98 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Memphis, TN
Posts: 195 Sonic98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 44 sec
Reputation Power: 6
Send a message via AIM to Sonic98 Send a message via Yahoo to Sonic98
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Array of Struct or Class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway