Thread: C++ Help with cin.getline

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    C++ Help with cin.getline

    I'm trying to get an array of names stored into a data structure, but I keep getting this odd error of C2661

    (error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    )

    I can't figure out how to fix this error, any help would be appreciated.(code included for reference)
    Code:
    #include <iostream>
    using namespace std;
    
    struct StudentRecords
    {
    char name;
    };
    
    void main()
    {
    StudentRecords student[5];
    for (int s = 0; s < 2; s++)
    {
    cout << "Enter student name ----> ";
    cin.getline(student[s].name );
    cout << endl;
    }

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
    //do this ...
    #include <string>
    
    // and this in struct:
    string name;
    and if you can't understand why, you need to learn a little more C++.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Already tried that, still get the same error of

    error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]

    with
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //structures
    struct StudentRecords
    {
    	string name;
    };
    
    void main()
    {
    
       StudentRecords student[5];
       for (int s = 0; s < 2; s++)
       {
       cout << "Enter student name ----> ";
       cin.getline(student[s].name);
       cout << endl;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The getline() overloaded for std::string takes two parameters: the istream and the string object.

    As such, you should write:
    Code:
    getline(cin, student[s].name);
    Note that void main() should be int main(), and that you should avoid placing using directives before struct/class definitions since they are likely to be placed in header files, and using directives should not be used in header files except in a local scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    a quick question:
    why cin.getline() takes the size of the string to read?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Quote Originally Posted by manav View Post
    a quick question:
    why cin.getline() takes the size of the string to read?
    Because it's late, and my brain is farting.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    why cin.getline() takes the size of the string to read?
    Because it takes a pointer to an array of char (a C style string), so it needs to know when to stop writing so as to avoid buffer overflow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    laser, why would it only be accepting the first cin of the character string and not allowing me further input, ex. I change the loop to 5 instead of two

    output
    Enter Student Name ----> Suzy Snoddgrass

    Enter Student Name ---->

    Enter Student Name ---->

    Enter Student Name ---->

    Enter Student Name ---->

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    laser, why would it only be accepting the first cin of the character string and not allowing me further input, ex. I change the loop to 5 instead of two
    It should not be doing that. What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //structures
    struct StudentRecords
    {
    	
    	string name;
    	int id;
    	int scores[4];
    	double avg;
    	char grade;
    
    };
    
    StudentRecords getStudent();
    void fillRecs( StudentRecords [], int);
    void display( const StudentRecords [], int);
    
    
    void main()
    {
    //Declare and Allocate Record
       StudentRecords student[5];
       fillRecs(student, 5);
       display(student, 5);
    
    }
    	
    StudentRecords getStudent()
    {
    	StudentRecords student;
    	
    	double a = 0;
    	cout << "Enter student name ----> ";
    	getline(cin, student.name);
    	cout << endl;
    	cout << "Enter student ID number ---> ";
    	cin >> student.id;
    	cout << endl;
    	cout<<"Enter the student scores: ";
    	for(int i=0; i<4; i++)
    	{	
    		cin>>student.scores[i];
    		a+= student.scores[i];
    	}	
    	a = a/4;
    	student.avg = a;
    	cout<<endl;
    
    	return student;
    }
    
    void fillRecs( StudentRecords r[], int n)
    {
    
       for( int i=0; i<n; i++)
          r[i] = getStudent();
    } 
    
    void display( const StudentRecords r[], int n) 
    {
     for( int j=0; j<n; j++)
     {
       cout << "Name " << r[j].name << endl;
       cout<<"id: "<<r[j].id<<endl;
       cout<<"Scores: ";
       for(int i=0; i<4; i++)
          cout<<r[j].scores[i]<<"  ";
       cout<<endl;
       cout<<"avg: "<<r[j].avg<<endl;
      }
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that cin>>student.scores[i] leaves the newline in the buffer so the next getline() gets that newline instead of what the user is supposed to enter. You could remove this newline with cin.ignore().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    6
    Thank you very much for your help laser.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You were told to correct that void main.
    http://cpwiki.sourceforge.net/Void_main
    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.

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    is it still needed in c++?
    as you told me, c++ does need main to return. so can we not use void main in c++?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    as you told me, c++ does need main to return. so can we not use void main in c++?
    The C++ standard defines the global main() function as returning an int. It also states that in the absence of an explicit return, 0 will be returned at the end of main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 06:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 05:53 PM