samedi 11 mars 2017

C++11 : cin doesn't wait for user input and ends the loop

My program take tuple data and stores into vector. There is no limit at how many tuples can be added and vector size changes with the adding of tuples. So I every time the program asks the user if he wants to add another tuple. If no, exits the loop and if yes, jumps back into the loop. My problem is, when the program was supposed to wait for my 'yes' or 'no' input, it skips the cin and exits the loop. I have tried searching for solutions, tried clearing the buffer but I am back to square one.

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

void print(vector<tuple<int, string, string, bool>> const & data) {
  if(data.size() != 0){
    for(auto row : data) {
        cout << get<0>(row)<<", " << get<1>(row)<<", "  << get<2>(row)<<", "<< get<3>(row)<<endl;
    }
}
else{
    cout <<"\nNO ENTRIES CURRENTLY!";
}
}
int main(){
 int id;
 char go;
 string name, filePath;
 bool flag = false;
 typedef tuple<int,string,string,bool>pData;
 vector<pData>pList;

 do{
  cout<<"Enter a Pattern Call:"<<endl;
  cin >> id >>name>>filePath>>flag;

  pList.push_back(make_tuple(id,name,filePath,flag));

  cout <<"Do You wish to add another: ";
  cin.ignore();
  cin >> go; //The control skips here and exits the loop.
 }while(go == 'y'|| go == 'Y');

  print(pList);

 return 0;
}

Please help me!

Aucun commentaire:

Enregistrer un commentaire