int main(){
loadwords();
vector <tuple> key;
cout << "input key sets(. to stop)"<<endl;
while (1){
int x[2];
cin>> x[0]>>x[1];
tuple y;
y.x=x[1],y.y=x[0];
if (x[0]=='.') break;
key.push_back(y);
}
return 0;
}
After I type '.'(without the ''), my computer goes unresponsive and I have to force shutdown. Would anyone have a clue why this is happening? Tuple is a simple struct that consists of char x and char y. loadwords() is a function that loads a file of words into a vector of strings. I am using GCC compiler on OpenSuSe linux.
thanks.
Your x[]s are ints, not chars, so you cannot enter '.' to stop. When you do enter '.', cin's state is set to 'fail' and no more input can be obtained -- meaning your loop will never break and your 'key' vector will continue to increase in size until crash.
The proper way to deal with this is to accept strings as input, and try to convert them to numbers (but only after checking to see that the user isn't finished).