Thread: Question about the getline function

  1. #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!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  3. #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 '.'

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Unhappy

    I really need help!

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Last edited by Dino; 11-03-2009 at 09:51 AM. Reason: typo
    Mainframe assembler programmer by trade. C coder when I can.

  6. #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

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM