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:

  1. (int) key;
  2. (string) name;
  3. (int) age;
  4. (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

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -