Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm new to C++.

C++
void getName(std::string &name, const int &playerNum){
    cout<<"Enter name for player"<<playerNum<<": ";
    std::getline(cin,name);
}

void player(){
    std::string playerOne, playerTwo;
    getName(playerOne, 1);
    getName(playerTwo, 2);
}

int main(){
    player();
    return 0;
}

In the program it did not wait for getName(playerOne,1) but instead continue to getline for getName(playerTwo,2). Why it happen?
I really think that I actually missed something simple here.

What I have tried:

previously I tried
C++
std::string getName(std::string &name, const int &playerNum){
    cout<<"Enter name for player"<<playerNum<<": ";
    std::getline(cin,name);
    return name;
}

but it produces segmentation error. Which I still didn't understand why it happen.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Feb-16 13:32pm    
Just think at this: how a function named getSomething could be void? why passing playerNum (especially int) of you are not returning any value to it? What's the point?
—SA
Are Riff 9-Feb-16 19:31pm    
There's not much point to this code except for me understand the related C++ rules. I read few pages of C++ book and try writing some code of my own. The process repeated until I finish the book. Since the book couldn't answer some questions I have, so here I am, asking question, while trying to learn some new points you might kindly points out to me.

I passed playerNum as const ref to show that it won't return any value to it. From what I've read, it just a good practice.
If i'm wrong, tell me I'm wrong.
Sergey Alexandrovich Kryukov 9-Feb-16 21:04pm    
Just use the debugger.
—SA

I just tried your code and it works fine. Are you sure you have copied the exact code from your program?. Also you do not need the ampersand (&) on the playerNum field as it is a constant.
 
Share this answer
 
Comments
Are Riff 9-Feb-16 19:14pm    
Enter name for player1: Enter name for player2:

This is the result in my code. It doesn't give me chance to enter name for player1 but instead just display it and skip it straight to player2.
Richard MacCutchan 10-Feb-16 3:32am    
I just tested this again and it works fine. I can only guess that the code you are running is different from the code you posted here, in some way. Please go back and look at it carefully line by line.
Try to clear any line in the buffer first before std::getline:
C++
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


Good luck!
 
Share this answer
 
Comments
Are Riff 9-Feb-16 19:32pm    
It kinda work but not 100%. Anyway, what just happen? what is this line? never seen one.
E.F. Nijboer 10-Feb-16 5:29am    
This removes any newline characters that are still in the inputbuffer before calling the getline (because it would otherwise read the getline and be done).
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

[Update]
Launch your program with the debugger and execute 1 line at the time.
and see what happen when you are in getName(playerOne, 1), see if the message is displayed as expected or not, idem for input.
 
Share this answer
 
v2
Comments
Are Riff 9-Feb-16 20:49pm    
Thanks, I've tried using debugger, but since i'm no expert using debugger, still couldn't explains why that bit of line behave certain way. It might as well I didn't use any :(
If getline fails then you've got some junk ending in a newline in the input buffer for the istream you're using it on. It might be as simple as you bashing return twice by accident when you run the code from the command line or your IDE doing something a bit strange when you invoke your program from inside it. An older (actually antique by now) version of Visual Studio had a habit of sticking a spurious return when invoking a program leading to the sort of thing you're seeing.

You also see this sort of thing happen when you mix extraction operators (>>) with getline. Most extraction operators work up to the first whitespace character. As most terminals are line oriented (input doesn't get passed to a program until you press return) if you do something like:

C++
int n = 0;
std::cin >> n;
std::string name;
std::getline( std::cin, name );


and then the user enters an integer for n, presses return and wonders why the string for name is empty.

Basically it's get the hang around the way terminals work, how iostreams work and how they integrate with whatever you're using to invoke your program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900