1,105,578 Community Members

Reading input

Member Avatar
losh177
Junior Poster in Training
55 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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....

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
0
 

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.

Member Avatar
losh177
Junior Poster in Training
55 posts since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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]
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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

Did you try it? What happened?

Member Avatar
mvmalderen
Nearly a Posting Maven
2,370 posts since Feb 2009
Reputation Points: 2,071 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 40 [?]
 
0
 

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

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

Question Answered as of 3 Years Ago by Lerner, WaltP and mvmalderen
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article