Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:00:08+00:00 2026-06-13T13:00:08+00:00

I got this strange problem while writing code for an exercise. First when I

  • 0

I got this strange problem while writing code for an exercise.

First when I pick the first option, enter a faulty entry. It’s supposed to go to an else branch of my code but instead it gets stuck in there. I really don’t know why. This also happens when I enter a “game title” with a space in between.

Secondly the line I commented out at the delete branch:

iter = gameTitles.erase(iter);

… doesn’t work at all. What I’m trying to do is deleting an entry by typing it out and after that comparing it to an entry so it knows what to delete. That’s why I’m also using the iterator.

// Exercise 1
/*
Write a program using vectors and iterators that allows a user to maintain a list of
his or her favorite games. The program should allow the user to list all game titles,
add a game title, and remove a game title.
*/

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;

int main(){
    bool    bLoop   = true;
    int     nChoice;
    char    cChoice;
    string  sInput;

    vector<string>::const_iterator iter;
    vector<string> gameTitles;

    while(bLoop){
        // -Head
        cout << "///////////////////////////////////\n// My Favorite Games\n\n";
        cout << "1. Add title\n2. Delete title\n3. Clear list\n\n";

        // -List
        if(!gameTitles.empty()){
            for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
                cout << "-" << *iter << endl;
            }
        }

        cout << "\n:: ";
        cin  >> nChoice;

        // 1. Add
        if(nChoice == 1){
            cout << "\nGame Title: ";
            cin >> sInput;
            gameTitles.push_back(sInput);
        }

        // 2. Delete
        else if(nChoice == 2) {
            cout << "Delete Title: ";
            cin >> sInput;

            for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
                if(*iter == sInput){
                    cout << "erased";
                    //iter = gameTitles.erase(iter);
                }
            }
        }

        // 3. Clear
        else if(nChoice == 3){
            cout << "Are you sure? (y/n) ";
            cin >> cChoice;
            if(cChoice == 'y'){
                gameTitles.clear();
            }
        } else {
            cout << "\nInvalid Choice, Please try again.\n";
        }

        // -Clean
        system("PAUSE");
        system("cls");
    }
}

EDIT: Solved first issue. Used a normal iterator instead of a constant one

EDIT2: Solved second issue, here is my corrected code:

// Exercise 1
/*
Write a program using vectors and iterators that allows a user to maintain a list of
his or her favorite games. The program should allow the user to list all game titles,
add a game title, and remove a game title.
*/

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;

int main(){
    bool    bLoop   = true;
    int     nChoice;
    char    cChoice;
    string  sInput;

    vector<string>::iterator iter;
    vector<string> gameTitles;

    while(bLoop){
        // -Head
        cout << "///////////////////////////////////\n// My Favorite Games\n\n";
        cout << "1. Add title\n2. Delete title\n3. Clear list\n\n";

        // -List
        if(!gameTitles.empty()){
            for(iter = gameTitles.begin(); iter!=gameTitles.end(); ++iter){
                cout << "-" << *iter << endl;
            }
        }

        cout << "\n:: ";
        cin >> nChoice;

        if(cin.fail()){
            cin.clear();
            cin.ignore();
        }

        // 1. Add
        if(nChoice == 1){
            cout << "\nGame Title: ";
            cin >> sInput;
            gameTitles.push_back(sInput);
        }

        // 2. Delete
        else if(nChoice == 2) {
            cout << "Delete Title: ";
            cin >> sInput;

            for(iter = gameTitles.begin(); iter!=gameTitles.end(); ){
                if(*iter == sInput){
                    cout << "erased";
                    iter = gameTitles.erase(iter);
                } else {
                    ++iter;
                }
            }
        }

        // 3. Clear
        else if(nChoice == 3){
            cout << "Are you sure? (y/n) ";
            cin >> cChoice;
            if(cChoice == 'y'){
                gameTitles.clear();
            }
        } else {
            cout << "\nInvalid Choice, Please try again.\n";
        }

        // -Clean
        system("PAUSE");
        system("cls");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:00:09+00:00Added an answer on June 13, 2026 at 1:00 pm

    There are actually two questions:

    1. Why does my input get stuck if enter something faulty? You need to verify that you input was successful using something like if (std::cin >> nChoice) { /* actual processing */ }. Note that the value of nChoice won’t change when the input fails. If the input failed, you need to do some error recovery: The stream has gone into fail state (i.e., std::ios_base::failbit is set in the error flags) and won’t refuse to do any further input until it got clear()ed. This still leaves the offending character(s) in the input which you may want to ignore().
    2. Why is the loop using erase() misbehaving? When you actually do erase() a value you don’t want to increment the iterator again at the end of the loop. If you do, it may very well move the iterator beyond the end resulting in undefined behavior. That is, the loop should look something like this:

      for(iter = gameTitles.begin(); iter!=gameTitles.end(); ) {
          if (*iter == sInput) {
               cout << "erased";
               iter = gameTitles.erase(iter);
          }
          else {
              ++iter;
          }
      }
      

      Of course, a short version of the same logic is this:

      gamesTitles.erase(std::remove(gamesTitles.begin(), games.Titles.end(),
                                    sInput),
                        games.Titles.end());
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’ve got this strange problem whereby the content within a scroll viewer increases in
I've Typo3 4.5.5 and Templavoila 1.5.5 installed and I got this strange problem in
This is a very strange problem. I've got a rails app in which I
I have a strange problem this time. I've got a masking database setup that
I got this strange issue while working with iPhone SDK 3.1.2. My UITableViewCell contains
I've got this strange problem in JBOSS 6, Eclipse, JSF MOJARRA. So this is
I've got this strange situation going on. Imagejpeg is only working when I define
Got this error message while trying to load view: The model item passed into
Got this line of code here but its not working. private void Button_Click(object sender,
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.