c++ - I am trying to use a do while loop to repeat a certain portion of my program and it refuses to execute properly -


okay title said refusing execute stuff right under "do" function though far can tell parameters repeat have been fulfilled. far when run program along lines of... "would search name? please enter y yes , n no:" looping on , on when press y

#include <iostream> #include <string> #include <iomanip> #include <vector> #include <algorithm> #include <cstdlib> using namespace std;  int main() {     vector <string> vname, vid, vclass;     string sname, sid, sclass, ssearch, cquestion;     int isize, istudent;     // display initial vector size     isize = vname.size();      cout << "student list starts size:" << isize << endl;      // size of list user      cout << "how many students add?" << endl;     cin >> istudent;     cin.ignore();     // names, ids, , classes      (int = 0; < istudent; i++)     {         cout << "student" << + 1 << ":\n";         cout << "please enter student name: ";         getline(cin, sname);         vname.push_back(sname);          cout << "enter id number ";         getline(cin, sid);         vid.push_back(sid);          cout << "enter class name ";         getline(cin, sclass);         vclass.push_back(sclass);      }     // display header      cout << "the list of students has size of: " << istudent << endl;     cout << "the student list" << endl;     cout << "\n";     cout << "name:" << setw(30) << "id:" << setw(38) << "enrolled class : " << endl;     cout << "--------------------------------------------------------------------------";     cout << "\n";      // loop displying list     (int x = 0; x < vname.size() && vid.size() && vclass.size(); x++)     {          cout << vname[x] << "\t \t \t" << vid[x] << "\t \t \t" << vclass[x] << endl;     }      // sorting function     cout << "\n";     cout << "the student list after sorting:" << endl;     cout << "\n";      sort(vname.begin(), vname.end());      (int y = 0; y < vname.size(); y++)     {         cout << vname[y] << endl;     }      cout << "\n";      // search function          {         cout << "please enter name searched:" << endl;         getline(cin, ssearch);         if (binary_search(vname.begin(), vname.end(), ssearch))         {               cout << ssearch << " found." << endl << endl;         }          else         {              cout << ssearch << " not found." << endl << endl;           }          cout << "would search name?" << endl << endl;          cout << "please enter y yes , n no:" << endl << endl;         cin >> cquestion;      } while (cquestion == "y" || cquestion == "y");       cout << "thank using program!" << endl;      return 0; } 

edit: posted whole program, please excuse grammatical mistakes, i'm trying program down before go in there , make pretty.

the tail of loop this:

cout << "please enter y yes , n no:" << endl << endl; cin >> cquestion; 

which consume string if entered one, leave trailing newline in input stream. when return top of loop after entering y or y, , this:

cout << "please enter name searched:" << endl; getline(cin, ssearch); 

the getline extract empty line.

how consume unread newline input stream you. end using .ignore() did prior in program. or use getline consume cquestion. have options. pick 1 works.

and side note, strongly advise check stream operations success before assuming "just worked". hard, necessary, habit break. this:

do {     cout << "please enter name searched:" << endl;     if (!getline(cin, ssearch))         break;      if (binary_search(vname.begin(), vname.end(), ssearch))     {         cout << ssearch << " found." << endl << endl;     }     else     {         cout << ssearch << " not found." << endl << endl;     }      cout << "would search name?" << endl << endl;     cout << "please enter y yes , n no:" << endl << endl;  } while (getline(cin,cquestion) && (cquestion == "y" || cquestion == "y")); 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -