Comp Sci Cin.ignore remove first character

  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion revolves around an issue with reading user input in a turn-based game setup. The problem arises when the first input is read correctly, but subsequent inputs only capture part of the input string. A user resolved the issue by removing the cin.ignore() call before getline, which allowed the full input to be captured consistently. The conversation highlights the importance of managing input buffers correctly when switching between different input methods. Proper handling of cin and getline is crucial for ensuring accurate user input in interactive applications.
anonim
Messages
39
Reaction score
2
Homework Statement
-
Relevant Equations
-
Code:
if(count%2==0){ //for user 1
  cout<<"User 1"<<endl;
  cout << "Please enter your move: ";
  cin.ignore();
  getline(cin,str);
  //do something
}
else if (count%2!=0){ //for user 2
  cout<<"User 2"<<endl;
  cout << "Please enter your move: ";
  cin.ignore();
  getline(cin,str);
  //do something
}
Let's say move is A 2. When first time enter input, It read the 'A 2'. But otherwise it reads ' 2'. How can I fix this?
 
Physics news on Phys.org
Does removing cin.ignore() do what I think you want?

Output.png
 
Joshy said:
Does removing cin.ignore() do what I think you want?

View attachment 272313
Before this part, I was getting numbers elsewhere using cin. And I add cin.ignore after that and I deleted cin.ignore before getline and it fixed. I'm just seeing your answer, but thank you anyway.
 

Similar threads

Replies
3
Views
1K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
6
Views
3K
Replies
3
Views
1K
Replies
9
Views
3K
Back
Top