Cin.ignore remove first character

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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?
 
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.