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.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

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