Alright, here's a problem I can't seem to figure out. I'm very new at all this, so hopefully this problem isn't too ridiculous. Basically the gist of it is in a program I made for a class; it wouldn't let the input for the cin.getline be typed unless I put a cin.ignore after a cout statement. Now I was taught that you only use cin.ignore after a cin, and not a cout line. I simplified my problem and put it in a different project and it worked without having to use the cin.ignore after the specific cout statement. I was obviously confused at the contradiction.

Here's the code that uses the cin.ignore

#include<iostream>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;

int main ()
{
	const int NAME_LENGTH=25, HOW_WELL=12;
	char player1Name[NAME_LENGTH], player2Name[NAME_LENGTH], p1howWell[HOW_WELL], p2howWell[HOW_WELL];
	int minRange, maxRange, p1Guess1, p1Guess2, p1Guess3, p2Guess1, p2Guess2, p2Guess3, p1score, p2score, p3score;

	srand(static_cast<unsigned>(time(0)));

	cout<<"Enter the smallest number in the range: ";
	cin>>minRange;
	cout<<"Enter the largest number in the range: ";
	cin>>maxRange;
	cout<<endl;

	int p3rand1=rand()%(maxRange-minRange+1)+minRange, p3rand2=rand()%(maxRange-minRange+1)+minRange, p3rand3=rand()%(maxRange-minRange+1)+minRange;
	int goldenNumber=rand()%(maxRange-minRange+1)+minRange;

	



//Here is where the problem is:






        cout<<"Player 1"<<endl;
	cout<<"Enter your name: ";
	cin.ignore(); //this right here; without it it skips the user inputing their data
	cin.getline(player1Name, NAME_LENGTH);
	cout<<"How well are you going to do? ";
	cin>>p1howWell;
	cout<<"Enter your three guesses."<<endl;
	cout<<"Answers must be between "<<minRange<<"-"<<maxRange<<": ";
	cin>>p1Guess1>>p1Guess2>>p1Guess3;
	cout<<endl;

	cout<<"Player 2"<<endl;
	cout<<"Enter your name: ";
	cin.ignore();
	cin.getline(player2Name, NAME_LENGTH);
	cout<<"How well are you going to do? ";
	cin>>p2howWell;
	cout<<"Enter your three guesses."<<endl;
	cout<<"Answers must be between "<<minRange<<"-"<<maxRange<<": ";
	cin>>p2Guess1>>p2Guess2>>p2Guess3;
	cout<<endl;

	cout<<"Player 3"<<endl;
	cout<<"Player 3's name: Big John"<<endl;
	cout<<"Player 3 estimates it will do BEST"<<endl;
	cout<<"Player 3 guesses "<<p3rand1<<" "<<p3rand2<<" "<<p3rand3<<endl<<endl;

	cout<<"The Golden Number was "<<goldenNumber<<endl;
	cout<<"The Results"<<endl;
	cout<<left<<setw(25)<<"Name"<<setw(15)<<"Estimate"<<setw(18)<<"Guesses"<<setw(6)<<"Score"<<endl;

	cout<<setw(25)<<player1Name<<setw(15)<<p1howWell<<setw(6)<<p1Guess1<<setw(6)<<p1Guess2<<setw(6)<<p1Guess3<<setw(6);
	p1score=abs(goldenNumber-p1Guess1)+abs(goldenNumber-p1Guess2)+abs(goldenNumber-p1Guess3);
	cout<<p1score<<endl;

	cout<<setw(25)<<player2Name<<setw(15)<<p2howWell<<setw(6)<<p2Guess1<<setw(6)<<p2Guess2<<setw(6)<<p2Guess3<<setw(6);
	p2score=abs(goldenNumber-p2Guess1)+abs(goldenNumber-p2Guess2)+abs(goldenNumber-p2Guess3);
	cout<<p2score<<endl;
	
	cout<<setw(25)<<"Big John"<<setw(15)<<"BEST"<<setw(6)<<p3rand1<<setw(6)<<p3rand2<<setw(6)<<p3rand3<<setw(6);
	p3score=abs(goldenNumber-p3rand1)+abs(goldenNumber-p3rand2)+abs(goldenNumber-p3rand3);
	cout<<p3score<<endl<<endl;

	return 0;
}

Now here's a simplified version of the previous code that doesn't use cin.ignore and doesn't skip the user typing the input

#include<iostream>
using namespace std;

int main()
{
	const int NAME_LENGTH=25;
	char player1Name[NAME_LENGTH], p1howWell[NAME_LENGTH];

	cout<<"Player 1"<<endl;
	cout<<"Enter your name: "; //no cin.ignore and it doesn't skip user input unlike in the other code
	cin.getline(player1Name, NAME_LENGTH);
	cout<<"How well are you going to do? ";
	cin>>p1howWell;
	cout<<"Enter your three guesses."<<endl;

	return 0;
}

I mean yeah both programs work, but it'd be nice to know why they need to be treated different for seemingly similar coding.

Recommended Answers

cin.ignore has absolutely nothing to do with cout. Nothing. It has to do with cin.
When you input a number using cin, you hit a RETURN to allow the number to be read. But the RETURN is left in the input buffer to be read on the next input -- …

Jump to Post

All 3 Replies

cin.ignore has absolutely nothing to do with cout. Nothing. It has to do with cin.
When you input a number using cin, you hit a RETURN to allow the number to be read. But the RETURN is left in the input buffer to be read on the next input -- like getline(). That RETURN is now read and the getline() continues as defined. That's why the cin.ignore is used.

I had a similar problem which I think I posted here. The recommended solution, which worked, was to use a cin.clear() statement to empty the input buffer so cin >> can be used again. Why is cin.ignore the solution for this problem? Does cin.ignore also empty the input buffer?

EDIT: I see what's up now. Alright, nevermind.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.