Getline(cin, ...) doesnt work!?

Hello programmers, i have a very simple and trivial, but although a not simply to be solved problem:

I have got a function:
1
2
3
void getname(string name) {
getline (cin, name);
}

I have included string, iostream etc and used namespace std,
So the compiler doesnt show an error.
But when i use the getname function in my program,
in the .exe file at the moment when it should get the name from user input, it does... Nothing .
So... If i list another thing after getline , e.g. a cout of the entered name: it does nothing -> it just skips the getline!

WHY????
Last edited on
You are supposed to be passing variables like this:
1
2
3
4
5
6
7
8
9
10
void getname()
{
getline(cin, name);
void showname(name); 
}

void showname(string n)
{
cout << n; 
}

Edit:
1
2
3
4
void getname(string name)//Pass variable to function
{
getline (cin, name); //Immediately overwrite passed variable
}
Last edited on
Arguments passed by value and by reference http://www.cplusplus.com/doc/tutorial/functions/
I knew all that too but my problem is not to build that func or to use it but that the console application does nothing when its at the turn of getline.
Note: im using Orwell Dev C++ 4.9.9.2
closed account (o1vk4iN6)
Orwell Dev C++ 4.9.9.2

That ide is from 2005, i would suggest you stop using it.

And the getline seems to work fine even though the function is questionable.
http://ideone.com/YivovU
So what is the recent one?
closed account (o1vk4iN6)
Updating to a newer version isn't going to solve your problem (not even sure there is one anyways). Though using a more recent compiler would be recommended (devcpp isn't a compiler).

I've never seen getline function in the manor you've described it other than: Are you building a window application that has no console ? Are you calling the correct getname function ?
Last edited on
> I knew all that too
> but my problem is not to build that func or to use it
If you know about it then you wouldn't have this issue.
You are passing the string by value, then you simply discard its content and fill it with the user input.
The parameter is useless like that.

I suppose that your intention was to pass by reference.


> it does nothing -> it just skips the getline!
maybe you leave a '\n' in the buffer.
Try cin.ignore();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

void getname_value(std::string name)
{
    std::cout << "name in getname_value: " << &name << '\n' ;
    std::getline(std::cin, name) ;
}

void getname_ref(std::string& name)
{
    std::cout << "name in getname_ref: " << &name << '\n' ;
    std::getline(std::cin, name) ;
}


int main()
{
    std::string name ;

    std::cout << "name in main: " << &name << '\n' ;

    getname_value(name) ;
    std::cout << "After getname_value name is \"" << name << "\"\n" ;

    getname_ref(name) ;
    std::cout << "After getname_ref name is \"" << name << "\"\n" ;  
}


http://ideone.com/jt1nCs

Notice that the address of the object in getname_value is different than that in main, meaning that it is a different object.
Last edited on
I fixed it now; there had to be
cin.ignore(0);
Before the getline func
Topic archived. No new replies allowed.