Reading input
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....
Related Article: Reading input from file
is a solved C++ discussion thread by Aprentchacker that has 5 replies and was last updated 3 years ago.
losh177
Junior Poster in Training
54 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.
Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 8
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]
losh177
Junior Poster in Training
54 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
How would i clear the buffer? Can i use the call cin.ignore('/n')?
Did you try it? What happened?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
Can i use the call cin.ignore('/n')?
First of all it is cin.ignore('[B]\[/B]n'); and secondly: read this ...
mvmalderen
Posting Maven
2,612 posts since Feb 2009
Reputation Points: 2,221
Solved Threads: 280
Skill Endorsements: 36
Question Answered as of 2 Years Ago by
Lerner,
WaltP
and
mvmalderen