Read from stdin in C++ -
here code:
... ... { cin >> command; switch(command) { case 'i': cin >> key >> nome >> idade >> endereco; count++; pessoas = (pessoa **) realloc(pessoas, count*sizeof(pessoa *)); pessoas[count-1] = new pessoa(key, nome, idade, endereco); btree->add(pessoas[count-1]); break; case 's': cin >> key; tosearch = (pessoa *) btree->search(key); if(tosearch == null) { cout << "-1" << endl; } else { cout << key << endl; cout << tosearch->getnome() << endl; cout << tosearch->getidade() << endl; cout << tosearch->getendereco() << endl; } break; case 'e': done = true; break; } } while(!done); ... ...
i have "menu" when enter i
, insert in btree, s
search , e
exit program.
the problem is, when hit i
insert, have give 4 parameters:
- (int) key;
- (string) name;
- (int) age;
- (string) address;
when give name have e
character inside (eg. "james"), exit program.
how avoid command
read stdin buffer while i'm reading on switch?
sample input:
i 1 joao da silva 1 11 rua 2, 3 2 joao da silva 2 12 rua 4, 6 s 1 s 7 e
thanks.
how std::basic_istream::get
(definition 1)?
that make switch statement start follows.
do { switch(cin.get()) {
bear in mind may prudent check std::char_traits::eof
in switch
in general case (though shouldn't necessary stdin).
Comments
Post a Comment