C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-02-2009, 10:29 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 5
Question about the getline function

I am trying to use the getline function in order to have a user input a students name. The code is as followed.

The Structure:

Code:
struct student	// the student structure meant to hold all student data 
				// in a linked list fashion
{
	char	studentNames[maxStudents][lnName];	//	student names
	int sectionNum;								// section the student is in
	float project[11];							// student's projects
	float test[3];								// students's tests
	float quiz[5];								 // students's quizzes

	student *nxt;								 // pointer to next student
}; // end struct student

The Function:
Code:
void	getStudentNames (char pgmID[],		char title[], char subTitle[], 
						 char gradebookName[]	)
{
	//	Declare local vbls for holding data that is inputted by the user.
	int numStudents;
	student *temp;

	//	Display title and sub-title lines.
	system ("cls");
	cout	<<	pgmID	<<	setw ( 40 + strlen(title) / 2 - strlen(pgmID)) << title	<<	endl;
	cout	<<	setw ( 40 + strlen(subTitle) / 2) << subTitle << "\n\n\n";

	//	If warranted, open the file for output.
	ofstream	gradeBookFile;
	gradeBookFile.open (gradebookName, ios::out);

	//	Input data for this item from the user and output it to the database.	
		cout << "\t\tPlease enter the number of students: ";
		cin >> numStudents;
		cin.ignore();

		//	Input data for this item from the user and output it to the database.
		for ( int count = 0; count < numStudents; count++ )
		{
			
			cout	<<	"\n\t\tPlease enter student name (L F MI): ";
			temp = new student;
			cin.getline ( temp[count] ->studentNames, lnName );
			
			gradeBookFile << temp->studentNames[count] << endl;
		} // end for ( int count = 0 ....

	
	//	Close the file, return to sender.
	gradeBookFile.close();
}	//	end getStudentNames (...
The error is at the getline function.

I don't know how to properly do this HELP PLEASE!
jmg0880 is offline   Reply With Quote
Old 11-02-2009, 10:42 PM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
And the error is.... ????


By the way, your struct should look like this:
Code:
struct student	// the student structure meant to hold all student data 
				// in a linked list fashion
{
	char	studentNames[maxStudents][lnName];	//	student names
	int sectionNum;								// section the student is in
	float project[11];							// student's projects
	float test[3];								// students's tests
	float quiz[5];								 // students's quizzes

	struct student *nxt;								 // pointer to next student
}; // end struct student
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is online now   Reply With Quote
Old 11-02-2009, 10:47 PM   #3
Registered User
 
Join Date: Nov 2009
Posts: 5
The error is :

error C2819: type 'student' does not have an overloaded member 'operator ->'

and also

error C2232: '->student::studentNames' : left operand has 'struct' type, use '.'
jmg0880 is offline   Reply With Quote
Old 11-03-2009, 08:47 AM   #4
Registered User
 
Join Date: Nov 2009
Posts: 5
Unhappy

I really need help!
jmg0880 is offline   Reply With Quote
Old 11-03-2009, 08:51 AM   #5
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
I am home sick today - so this is the best I care to offer right now.

When working with structs, when you have one statically allocated, you use dot notation to access its members.

When working with pointers to structs, you use arrow notation.
__________________
Mac and Windows cross platform programmer. Ruby lover.

Memorable Quotes From Recent Posts:

I can't remember.

Last edited by Dino; 11-03-2009 at 09:51 AM. Reason: typo
Dino is online now   Reply With Quote
Old 11-03-2009, 09:10 AM   #6
Registered User
 
Join Date: Nov 2009
Posts: 5
Quote:
Originally Posted by Dino View Post
I am home sick today - so this is the best I care to offer right now.

When working with structs, when you have one statically allocation, you use dot notation to access its members.

When working with pointers to structs, you use arrow notation.
Thanks man, I will try that tonight and let you know how it works out
jmg0880 is offline   Reply With Quote
Old 11-03-2009, 12:35 PM   #7
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
Quote:
Originally Posted by Dino View Post
And the error is.... ????


By the way, your struct should look like this:
Code:
struct student	// the student structure meant to hold all student data 
				// in a linked list fashion
{
	char	studentNames[maxStudents][lnName];	//	student names
	int sectionNum;								// section the student is in
	float project[11];							// student's projects
	float test[3];								// students's tests
	float quiz[5];								 // students's quizzes

	struct student *nxt;								 // pointer to next student
}; // end struct student
Huh, should be fine in C++ without the struct keyword in front of student. In C yes that would be a problem.




Code:
for ( int count = 0; count < numStudents; count++ )
{
    cout << "\n\t\tPlease enter student name (L F MI): ";
    temp = new student;
    cin.getline ( temp[count] ->studentNames, lnName );
You aren't creating arrays of students here so why are you attempting to access them like temp is an array? You are creating single instances each time through the loop. You also aren't doing anything to link the nodes together into a linked list.



Any errors in the above I attribute to the Vicodin I'm currently taking... good stuff.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 11-03-2009, 01:16 PM   #8
Registered User
 
Join Date: Nov 2009
Posts: 5
Quote:
Originally Posted by hk_mp5kpdw View Post
Huh, should be fine in C++ without the struct keyword in front of student. In C yes that would be a problem.




Code:
for ( int count = 0; count < numStudents; count++ )
{
    cout << "\n\t\tPlease enter student name (L F MI): ";
    temp = new student;
    cin.getline ( temp[count] ->studentNames, lnName );
You aren't creating arrays of students here so why are you attempting to access them like temp is an array? You are creating single instances each time through the loop. You also aren't doing anything to link the nodes together into a linked list.



Any errors in the above I attribute to the Vicodin I'm currently taking... good stuff.
I just want to be able to type in a full name and grabbing a char that is declared in the structure like, for example,

Billy Bob M
jmg0880 is offline   Reply With Quote
Old 11-03-2009, 01:19 PM   #9
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Then it would be
temp->studentNames[count]
The index comes after the actual variable whose elements you want to access.
Disclaimer: I have not checked this for rightness. I am simply explaining how it should be done.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
We Got _DEBUG Errors Tonto Windows Programming 5 12-22-2006 05:45 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Calling a Thread with a Function Pointer. ScrollMaster Windows Programming 6 06-10-2006 08:56 AM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM
qt help Unregistered Linux Programming 1 04-20-2002 09:51 AM


All times are GMT -6. The time now is 07:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22