Thread: Menu Numbering Problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Menu Numbering Problem

    The only problem I am running into when I run this program is that fact that my 2nd for loop doesn't print out 1,2,3 just 0,1,2. I understand why it prints out the second type but how do I fix it ? My first thought was to replace it with for(int i=1;i<3;++i) but that will skip my 0 position element all together or give me a run-time error

    If anyone can help me solve this problem It would be very helpful.

    Code:
    #include <iostream>
    #include<vector>
    #include<string>
    
    using namespace std;
    
    int main()
    {
    
       string game0,game1,game2;
    
       vector<string>myGames;
       vector<string>::iterator iter;
    
       cout<<"What is Your Favorite Game :";
       cin>>game0;
       cout<<"What is Your 2nd Favorite Game :";
       cin>>game1;
       cout<<"What is Your 3rd Favorite Game : ";
       cin>>game2;
    
    
       myGames.push_back(game0);
       myGames.push_back(game1);
       myGames.push_back(game2);
    
        cout<<"\n\t\tTop 3 Favorite Games\n";
    
        for( iter=myGames.begin(); iter!=myGames.end();++iter)
                cout<<". "<<*iter<<endl;
    
        int choice;
        char replace;
        cout<<"\nDo you want to replace a game (y/n) :";
        cin>>replace;
    
    while (replace=='y')
    {
       for(int i = 0; i<myGames.size();++i)
            cout<<i<<". "<<myGames[i]<<endl;
    
        cout<<"\nWhat is Your Choice :";
        cin>>choice;
    
        switch (choice)
    {
    
        case 1:                         myGames.erase(myGames.begin());
                                            cout<<"\nWhat's Your New #1 Game ? ";
                                            cin>>game0;
                                            myGames.insert(myGames.begin(),game0);
                                            break;
    
        case 2:                        myGames.erase(myGames.begin()+1);
                                            cout<<"\nWhat's Your New #2 Game ? ";
                                            cin>>game1;
                                            myGames.insert(myGames.begin()+1,game1);
                                            break;
    
        case 3:                        myGames.erase(myGames.begin()+2);
                                            cout<<"\nWhat's Your New #3 Game ? ";
                                            cin>>game2;
                                            myGames.insert(myGames.begin()+2,game2);
                                            break;
        default:            cout<<"Wrong Number Try Again\n";
                                            continue;
    }
    
        cout<<"\nDo you want to replace another (y/n) :" ;
        cin>>replace;
        cout<<"\n";
    }
        cout<<"\n\t***Top 3 Favorite Games***\n";
          for(int i = 0; i<myGames.size();++i)
            cout<<i<<". "<<myGames[i]<<endl;
    
    
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So don't print i; print i+1 instead.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    LOL didn't even know you could do that

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Using this code: (int i=1;i<3;i++) will not produce the number 3 ever, because your control statment runs the loop only when 'i' is LESS than 3. If you want to produce 3, your code should look like:

    (int i=1;i<=3;i++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Simple Menu Problem
    By DanC in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2006, 01:33 PM
  3. Problem with Mouse Over Menu!!! HELP!!!
    By SweeLeen in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2006, 02:10 AM
  4. Window - Menu problem...
    By FromHolland in forum Windows Programming
    Replies: 1
    Last Post: 02-26-2004, 03:49 PM
  5. MDI and MENU Problem
    By Marc in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2004, 06:59 PM