I'm having problems reading taking an input using cin.getlin

[LIST=1]
[*]cout << "Enter record: ";
[*]cin.getline(title, 100);
[*]csis << "Enter record title: " << title << endl;
[*]rec->setTitle(title);

[*]cout << "Enter performer: ";
[*]cin.getline(name, 100);
[*]csis << "Enter performer: " << name << endl;
[*]rec->setPerformer(name);
[/LIST]

The program jumps to the capture the "name" var without allowing the user to input the "title" var
This is how it looks in the console window

Enter record title: Enter performer:

Any thoughts....

Recommended Answers

All 4 Replies

The istream input buffer still has a newline char in it from some call to an istream method that doesn't clear terminating char, like >>, etc, when you call getline() the first time.

How would i clear the buffer? Can i use the call cin.ignore('/n')? This is my main code, if it is of any help:

[LIST=1]
[*]using namespace std;

[*]ofstream csis;

[*]const int size = 10;
[*]int ct = 0;
[*]Book *bk;
[*]Record *rec;
[*]Holding *holdLib[size];

[*]int main()
[*]{
[*]	csis.open("csis.dat");

[*]	char select;
[*]	char *title;
[*]	char *name;
[*]	int call;
[*]	char format;

[*]	cout << "Test entries: " << endl;
[*]	cout << endl;
[*]	csis << "Test entries: " << endl;
[*]	csis << endl;
[*]	
[*]	test1();
[*]	test2();
[*]	test3();
[*]	test4();
[*]	test5();

[*]	cout << endl;
[*]	cout << "Enter 5 holdings to be stored in a list: " << endl;
[*]	csis << endl;
[*]	csis << "Enter 5 holdings to be stored in a list: " << endl;
[*]		
[*]	for (int c = ct; c < 10; c++)
[*]	{
[*]		cout << endl;
[*]		csis << endl;

[*]		title = new char[100];
[*]		name = new char[100];
[*]		
[*]		cout << "Enter B for book, R for recording: ";
[*]		cin >> select;
[*]		csis << "Enter B for book, R for recording: " << select << endl;

[*]		if (select == 'B' || select == 'b')
[*]		{
[*]			bk = new Book;
[*]			
[*]			cout << "Enter book title: ";
[*]			cin.get(title, 100);
[*]			//cin >> title;
[*]			csis << "Enter book title: " << title << endl;
[*]			bk->setTitle(title);

[*]			cout << "Enter book author: ";
[*]			//cin >> name;
[*]			cin.getline(name, 100);
[*]			csis << "Enter book author: " << name << endl;
[*]			bk->setAuthor(name);

[*]			cout << "Enter call number: ";
[*]			cin >> call;
[*]			csis << "Enter call number: " << call << endl;
[*]			bk->setCall(call);
[*]			
[*]			cout << endl;
[*]			csis << endl;

[*]			holdLib[c] = bk;
[*]		}
[*]		else if (select == 'R' || select == 'r')
[*]		{
[*]			rec = new Record;

[*]			cout << "Enter record: ";
[*]			cin.get(title, 100);
[*]			//cin >> title;
[*]			csis << "Enter record title: " << title << endl;
[*]			rec->setTitle(title);

[*]			cout << "Enter performer: ";
[*]			cin.getline(name, 100);
[*]			//cin >> name;
[*]			csis << "Enter performer: " << name << endl;
[*]			rec->setPerformer(name);

[*]			cout << "Enter format: (L)P, (C)assette, (R)eel-to-Reel, (D)isk: ";
[*]			cin >> format;
[*]			csis << "Enter format (L)P, (C)assette, (R)eel-to-Reel, (D)isk: " << format << endl;
[*]			rec->setFormat(format);

[*]			cout << "Enter call number: ";
[*]			cin >> call;
[*]			csis << "Enter call number: " << call << endl;
[*]			
[*]			cout << endl;
[*]			csis << endl;
[*]			rec->setCall(call);
[*]			
[*]			holdLib[c] = rec;
[*]		}
[*]		else
[*]		{
[*]			cout << "You have enter the wrong selection: " << endl;
[*]			c--;
[*]		}
[*]		//delete [] title;
[*]		//delete [] name;
[*]	}

[*]	cout << endl;
[*]	cout << "Here are the holdings: " << endl;
[*]	cout << endl;
[*]	csis << endl;
[*]	csis << "Here are the holdings: " << endl;
[*]	csis << endl;

[*]	for (int n = 0; n < size; n++)
[*]	{
[*]		if (n == 5)
[*]		{
[*]			cout << endl;
[*]			csis << endl;
[*]		}
[*]		holdLib[n]->print();
[*]	}

[*]	csis.close();

[*]	return 0;
[*]}
[/LIST]

How would i clear the buffer? Can i use the call cin.ignore('/n')?

Did you try it? What happened?

Can i use the call cin.ignore('/n')?

First of all it is cin.ignore('[B]\[/B]n'); and secondly: read this ...

Be a part of the DaniWeb community

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