while(cin >> num)
The end condition for this loop is when the fail flag is set for the cin stream.
Before you can do any futher input from the stream, you need to clear the flag(s).
add this before the second input: cin.clear(); to clear the error flags
and cin.ignore(1000, '\n'); to get rid of the unwanted input from the buffer.
On line 12, you're using operator>> to extract ints from cin. But 'Q' is not an int, so if you enter it at the command line then the extraction will fail, putting cin into a fail state. Once that has happened, all subsequents cin calls will fail.
The quickest solution would be to select an int value to use as the sentinal. Perhaps -1? And use that to terminate the loop.
If you want to use Q to quit the loop, then you will need to read strings and then convert the numbers from the ascii string reprentation to int yourself. Using failure to terminate a loop is not a good idea.