C++, Iterator

Started by
7 comments, last by pi_equals_3 18 years, 8 months ago
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;
}

Advertisement
Im not quite sure why its displaying the arrow things wrong < i don't kno why its doing that
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);
Ok it worked but it won't let me put spaces in the game im adding.
am i sposed to like have some king of bonus thing added.
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'.
OOO thanx. I havn't learned that yet. thanx a bunch. :)
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.
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.
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!    ....

This topic is closed to new replies.

Advertisement