I've been trying to fix a problem with getline() and cin.getline().Here's a example I've made and it still has the problem:
" No matching function to 'getline(std::istream&, char[100])' "

Here's the code:

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main()
{
    char name[100];
    cout << "\nEnter your name: ";
    getline (cin, name);
    cout << "\nYour name is: " << name;
    Sleep (5000);
    
    return 0;
}

I don't know why but I think I know how to fix it, except I can't see the problem...:@

Member Avatar for jencas

Try

cin.getline(name, sizeof(name));

you can use either
cin.getline (name, '\n');
orn declare a
string name;
insread of character array.

thats what i mentioned in my previous post.
You can declare
string name;
instead of that character array. It will work

thats what i mentioned in my previous post.
You can declare
string name;
instead of that character array. It will work

Not quite. The syntax in slightly different when using getline() with std::strings:

string str;
getline (cin,str);

you can use either
cin.getline (name, '\n');

Oops - second parameter of the function is the size of destination array.

If you want to use a (non-default) delimiter, you must still enter the array size.

In your example, the getline would read in up the number of characters (less one) that is the ASCII value of the newline (10).

`

try out this my friend
    char string[20];

    cout<<"enter ur name";
    cin.getline(name,20);

    cout<<"ur name is :"<<name;

`

hello...
if i use cin.getline, it will difficult to detect cin.. so, i can't use cin and cin.getline simltnously.. so, how can i enter integer data to my programing by using cin.getline..

When using cin and cin.getline() together the order is important because the '\n' character can sometimes make input skip. Here is a simple example of how you could use them together in the right order and with clearing '\n' so it doesn't get in the way:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{


    char name[50];
    cout << endl << endl << "Enter Your First Name: ";
    cin >> name;

    cout << endl << endl << "You entered: ";
    for(unsigned short x =0; x < (strlen(name)); x++){
        cout << name[x];
    }


    string buffer;
    cout << endl << endl << "Press Enter to Continue...";
    getline(cin, buffer);
    cin.get(); // clear '\n' now


    string name2;
    cout << endl << endl << "Enter Someone Else's Name: ";
    cin >> name2;

    cout << endl << endl << "You entered: " << name2;


    cout << endl << endl;
    cout << endl << endl << "Press enter to continue";
    char c = _getch();
    return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.