Jump to content
  • Advertisement
Sign in to follow this  
KaiDao

Program crashes? Worked fine before...

This topic is 3750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I've just wrote a small program that let's you add the names of your favorite games and lets you rank them (lists, adds, removes, etc)...and it all worked fine before. But then on my fourth or fifth time of running the program, it says it crashes. I think what caused this is that when I tried to insert a game name with a space in it (i.e Grand Theft Auto), the program goes berserk and after that, whenever I rerun the program, it crashes. Any help? Here's the code:
//Favorite Games
//Uses vectors and iterators to store person's favorite games

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    cout << "Welcome to your Game Name manager!" << endl;
    
    vector<string> games;
    vector<string>::iterator myIter;
    vector<string>::const_iterator iter;
    
    while(true){
    cout << "Please choose an option : " << endl;
    cout << "1 - View current favorite games" << endl;
    cout << "2 - Add a new game" << endl;
    cout << "3 - Remove a game" << endl;
    cout << "4 - Quit Program" << endl;
    cout << "Choice : ";
    int choice;
    cin >> choice;
    cout << "" << endl;
    
    if (choice == 4)
       break;
       
    string gameName;
    int rank;
    
    switch(choice)
    {
    case 1:
         cout << "Here are a list of your favorite games : " << endl;
         if (games.empty())
            cout << "You currently have no favorite games!" << endl;
         for (iter = games.begin(); iter != games.end(); ++iter)
             cout << *iter << endl;
         break;
    case 2:
         cout << "Name of the game : ";
         cin >> gameName;
         cout << "Rank of the game : ";
         cin >> rank;
         games.insert((games.begin() - 1 + rank), gameName);
         break;
    case 3:
         cout << "Rank of the game : ";
         cin >> rank;
         games.erase((games.begin() - 1 + rank));
         break;
    default:
            cout << "You entered an invalid choice" << endl;
    }
    cout << "" << endl;
}
    return 0;
}
Thanks!!!

Share this post


Link to post
Share on other sites
Advertisement
Hmm..I changed the code to :


cout << "Name of the game : ";
getline(cin,gameName);
cout << "Rank of the game : ";
cin >> rank;
games.insert((games.begin() - 1 + rank), gameName);
break;


For case 2...but now it completely skips my input for the game name, and it crashes right afterwards :(

Share this post


Link to post
Share on other sites
You aren't checking if the insertion iterator is valid. If you're entering a rank that isn't 1, then it isn't going to work (for the first try). You can't insert it into a position 3 spots away from games.begin() if that memory hasn't been allocated. As for the skipping the name entry, try adding cin.ignore() right after cin >> choice.

Share this post


Link to post
Share on other sites
Quote:
Original post by KaiDao
but now it completely skips my input for the game name, and it crashes right afterwards :(


It's not skipping your input. Your input is blank.

Explanation: Input data from std::cin never "synchronizes" with your program: if you don't read all the text that's available, the remaining text will sit there, ready to be picked up immediately by the next read attempt - no matter how far in the future that is. Meanwhile, the console is buffering your input; it won't send anything to your program until it gets a whole line of text. This is an important feature: otherwise, you would have to handle backspaces yourself. Then it makes the whole line of text available, including the newline at the end.

Reading in with operator >> skips past any whitespace, reads the value, and stops right there. So if you type a number and hit return, the console will wait for the return, and then make that whole line available to your program; the program reads the number, and leaves the newline in the input buffer.

Reading in with std::getline reads up until the newline character (you can specify another character to read up until, but that doesn't really address your problem). It stops as soon as it sees that character. If it's the first character in input (as is the case here), it simply stops immediately, and the read-in text is an empty string. If this was possible using text already in the input buffer, then the program won't stop, because it has no reason to.

Next, the line you think is supposed to be your game name gets fed to the operator>> again to read the rank of the game, because the program has moved on to that point. Your input isn't numeric, so the read fails.

When reading fails, two things happen. More accurately, one thing happens and one does not.

What does happen is that an error flag is set on the stream (std::cin, here), such that every attempt to read again from the stream will fail until the flag is reset. This is deliberate and useful: it gives you a way to check for the error. (You can't just look at the value of the read-in variable; no matter what that value is, it could be one that actually was provided.)

What doesn't happen is any actual reading. All data is left in the buffer (or rather, restored to the buffer, when the reading process realises that it can't make anything valid out of the first however many characters). This means that
'rank' is now still uninitialized, and the indexing into the vector is, necessarily, bogus. However, just providing variable initializations isn't going to fix the program, because you'll still end up in an infinite loop with all the failed reads.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!