PDA

View Full Version : [SOLVED] Unusual Runtime Termination



Sinkingships7
October 8th, 2008, 12:22 AM
This program works as expected, save for one tiny flaw. On my Windows machine, after the user stops the program by entering the number 42, the command prompt spits this error at me:

"This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."

Here's the code, written in C++:

/* http://www.spoj.pl/problems/TEST/
*
* This program lets the user input a series of numbers, and doesn't stop
* until the user inputs the number 42. Afterward, all the numbers that the
* user input are displayed back (except for 42).
*/
#include <iostream>
#include <vector>

int main(){
std::vector <int> guesses(0);

while (true){
int guess;

std::cout << "Guess: ";
std::cin >> guess;

if (guess == 42){
break;
}

guesses.push_back(guess);
}

for (unsigned int index = 0; index <= guesses.size(); index++){
std::cout << guesses.at(index) << std::endl;
}

return 0;
}


Any ideas?

LaRoza
October 8th, 2008, 12:29 AM
Not sure about the error, but maybe:


while (true){
int guess;

std::cout << "Guess: ";
std::cin >> guess;

if (guess == 42){
break;
}

guesses.push_back(guess);
}


could be written better.



int guess;
do{
std::cout << "Guess: ";
std::cin >> guess;
guesses.push_back(guess);
} while (guess != 42)

Sinkingships7
October 8th, 2008, 12:35 AM
Nah, 'cause if guess == 42, then 42 is added to the end of my vector, and I don't want that. I could write it your way and just add a simple

guesses.pop_back();

after the loop, but meh.

LaRoza
October 8th, 2008, 01:03 AM
Nah, 'cause if guess == 42, then 42 is added to the end of my vector, and I don't want that. I could write it your way and just add a simple

guesses.pop_back();

after the loop, but meh.

How about:


#include <iostream>
#include <vector>

int main(){
std::vector <int> guesses(0);


int guess;
while (true)
{
std::cout << "Guess: ";
std::cin >> guess;
guess != 42 guesses.push_back(guess) ? break ;
}


for (unsigned int index = 0; index <= guesses.size(); index++){
std::cout << guesses.at(index) << std::endl;
}
return 0;
}

rogersce
October 8th, 2008, 01:09 AM
looks like the last loop should be

for (unsigned int index = 0; index < guesses.size(); index++){
std::cout << guesses.at(index) << std::endl;
}

rogersce
October 8th, 2008, 01:11 AM
in other words, if guesses.size() = 10, then guesses is indexed on [0,9].

if you do a for(i<=guesses.size()) then you'll index guesses.at(10), which is outside the bounds of guesses.

if you do for(i<guesses.size()) then you're indexing i=0 to 9 and staying within the bounds.

Sinkingships7
October 8th, 2008, 01:13 AM
looks like the last loop should be

for (unsigned int index = 0; index < guesses.size(); index++){
std::cout << guesses.at(index) << std::endl;
}

!!!

You, sir or ma'am, seem to be correct!

Such a primitive mistake, too...