Sign in to follow this  

C++, Iterator

This topic is 4516 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

Hi, im making this program that holds a list of favorite games and asks the user if they would like to add or subtract a game. but I am having trouble getting my program to display what they chose for a favortie game. here's the code:
// Video Game List
// Iterating and using Vectors

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

using namespace std;

int main()
{
    vector<string> list;
    list.push_back("Resident Evil 4");
    list.push_back("Halo 2");
    list.push_back("Star Wars Galaxies");
    
    vector<string>::iterator myIterator;
    vector<string>::const_iterator iter;
    
    cout << "Your Favorite games:\n\n"; //show favorite games
    for (iter = list.begin(); iter != list.end(); ++iter)
        cout << *iter << endl;
    
    char add;
    char subtract;
    char choice;
    char game;
    
    add = 'a';
    subtract = 's';
    cout << "\nPress 'a' to add a game or press 's' to subtract a game\n";
    cin >> choice;
    
    if(choice == 'a')
    {
    cout << "Enter the game name you would like to add.";
    cin >> game;
    cout << "\nYour Games are now:\n"; 
    //need to add 'game' to the list
    for (iter = list.begin(); iter != list.end(); ++iter)
        cout << *iter << endl;
    }else 
    
    cout << "You chose to subtract.";
    
    getch();
    return 0;
}

Share this post


Link to post
Share on other sites
Firstly game shouldn't be a "char" variable because you are storing more than one character, you are storing a string. So game should be defined as:

string game;

Then to add the game to the list you just do:

list.push_back(game);

Share this post


Link to post
Share on other sites
okay then to get the game name replace cin >> game; with getline(cin,game);

cin stops processing after the first whitespace is encountered. The getline function only stops at '\n'.

Share this post


Link to post
Share on other sites
Ok now I have another problem lol. When I put that in and ran it. It asked if you would want to add or subtract. but then when i wanted to add. It didn't let me type anything it just went straight threw everything.

Share this post


Link to post
Share on other sites

string add = "a";
string subtract = "s";
string choice;
string game;

cout << "\nPress 'a' to add a game or press 's' to subtract a game\n";
getline(cin,choice);

if(choice.compare(add) == 0) {
...


I replaced the add, subtract and choice variables with strings, used getline to get the choice, and used choice.compare to check against the add variable. It now works grand, at least on my machine.

Always use strings over individual chars, or c-strings, in situations like this. You are much less likely to get horrible bugs, and they are just plain easier to use.

Share this post


Link to post
Share on other sites
It's probably going straight through your input because of a common problem that comes up when using both extraction, as in 'cin >>' and getline.

Calling 'cin >> choice' reads until the end of the line (when you pressed 'Enter') and stops, leaving the newline character in the input stream. Getline immediately sees that it's already at a new line, so it returns an empty string for your title.

Stro's method will work, because unlike extraction, getline throws out the newline when it reaches it, so the next getline will wait for your input, and a new 'Enter' press.

An alternative would be using the same method as before combined with the function cin.ignore() which throws out characters without reading them...


add = 'a';
subtract = 's';
cout << "\nPress 'a' to add a game or press 's' to subtract a game\n";
cin << choice;
cin.ignore(); // Ignore one character, which is '/n', so the next getline will work as intended

if(choice == 'a')
{
cout << "Enter the game name you would like to add.";
getline(cin, game); // This works now because the extra \n was removed!
....

Share this post


Link to post
Share on other sites

This topic is 4516 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this