This code causes my computer to become unresponsive. Only STD librarys are used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
It is because you enter an infinite loop.

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).

Here are some old posts of mine that might help - it is worth your time to check them out. "Press ENTER twice to finish"
http://www.cplusplus.com/forum/beginner/2409/#msg9254
http://www.cplusplus.com/forum/beginner/2327/#msg8824

Hope this helps.
WOW, thanks. I noticed the input was wrong after awhile, but i never thought it would go into an Infinite loop.
Topic archived. No new replies allowed.