Cin.ignore remove first character

  • Comp Sci
  • Thread starter anonim
  • Start date
  • Tags
    C++
In summary, the conversation is about fixing an issue with reading user input. The users take turns inputting their moves, and the issue is that sometimes it reads only the number and not the letter before it. The suggested solution is to remove the use of cin.ignore(). The person who asked the question confirms that removing cin.ignore() before the getline() function fixed the issue.
  • #1
anonim
40
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
  • #2
Does removing cin.ignore() do what I think you want?

Output.png
 
  • #3
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.
 

1. How does cin.ignore remove the first character from input?

The cin.ignore function is used to ignore a certain number of characters from the input stream. When used with a value of 1, it will ignore the first character in the input stream, effectively removing it from the input.

2. What happens if I use cin.ignore without specifying a value?

If no value is specified, cin.ignore will ignore one character by default. This means that the first character in the input stream will be removed.

3. Can I use cin.ignore to remove more than one character?

Yes, cin.ignore can be used to ignore multiple characters from the input stream. You can specify the number of characters to ignore as the argument for the function.

4. Does cin.ignore remove the first character only from the first input?

No, cin.ignore will remove the first character from any input stream, regardless of whether it is the first or subsequent input. It will always ignore the first character in the input stream.

5. Is cin.ignore necessary when using cin.getline() to remove the first character?

No, cin.getline() automatically removes the first character from the input stream. Using cin.ignore is not necessary in this case.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
941
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top