Help - Search - Members - Calendar
Full Version: Learning c++
Pixel2Life Forum > Help Section > Desktop Programming
npsken
Hi, I am learning C++ (I have been for about 2 days now) and I am following the book "Beginning C++ Through Game Programming, Second Edition" by Michael Dawson. I am trying to do Exercise 1 in Chapter 4:

CODE
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 title, add a game title, and remove a game title.


Being as this is only an exercise and not a real world application, I don't need to be able to write the information to a file (I haven't even gotten far enough in the book to even know how to do that yet).

This is what I have so far:
CODE
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;

int main()
{
//setup
vector<string> games;

cout << "1. List Games\n";
cout << "2. Add Games\n";
cout << "3. Remove Games\n";
cout << "4. Quit\n";

int choice;
cout << "Choose an option: ";
cin >> choice;

while(choice == 1 || choice == 2 || choice ==3)
{
switch(choice)
{
case 1:
for(int i = 0; i < games.size(); i++)
cout << games[i] << endl;
cout << "Choose an option: ";
cin >> choice;
break;
case 2:
cout << "Enter game to add: ";
string game;
cin >> game;
games.push_back(game);
cout << "New List:\n";
for(int i = 0; i < games.size(); i++)
cout << games[i] << endl;
cout << "Choose an option: ";
cin >> choice;
break;
case 3:
cout << "List:\n";
for(int i = 0; i < games.size(); i++)
cout << (i + ". " + games[i]) << endl;
cout << "Enter number of game to remove: ";
int remove;
cin >> remove;
games.erase((games.begin() + remove);
cout << "New List:\n";
for(int i = 0; i < games.size(); i++)
cout << games[i] << endl;
cout << "Choose an option: ";
cin >> choice;
break;
}
}
//shutdown
cout << "Thank you for using chapter 4, exercise 1!";

return 0;
}


I include the algorithm header because I want to eventually use the sort function to display the list.

Anyway, here is what bloodshed compiler is saying:
CODE
C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp In function `int main()':
42 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp jump to case label
33 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp crosses initialization of `std::string game'
49 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp expected `)' before ';' token
42 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp [Warning] destructor needed for `game'
42 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp [Warning] where case label appears here
42 C:\Users\Kenneth Powers\Documents\Projects\C++\Examples\Chapter 4\exercises\1\1.cpp [Warning] (enclose actions of previous case statements requiring destructors in their own scope.)


Any help that can be provided would be greatly appreciated.

And also go easy on me if I made any code that doesn't make sense. I have been using c++ a minimal amount for 2 days and this is the first application I'm making without assistance from the book (besides it being an exercise, there are no instructions past what I have provided).

Edit: It is 0100 where I am right now and I need to get some sleep. Sorry if I don't reply quickly.
Encrypt
Well, this post is pretty late, by now you've probably eitheR:

1. Gave up on C++
2. Figured it out
3. Haven't figured it out, also haven't gave up on C++

here goes, there was two problems with your code:

most of the compiler errors had a simple solution, which was to do with the switch statements, for future reference, remember when initializing local variables inside the switch statement, they should be declared in their own local blocks such as:

CODE
case 1:
{
    int z;
}
case 2:
{
    int x;
}


if you don't do this, your compiler will think that the variables are in the main scope of the program, even if the case statement ends, which means when it gets to the case 2, a space in the stack has already been arranged for z but the value was not initialized. The other problem was only that you forgot a closing parenthesis at the end of one of the functions, which i will highlight in the fixed code. Last thing is, i don't know if it was the forum or just you, but when i pasted your code from the forum into dev-cpp, it wasn't indented at all, making it pretty hard to read, and also try to open for statements with braces like so:

CODE
for (x = 0; x < 10; x++)
{//FOR starts here
     //code here
}//FOR ends here


it's always nice to do this because often, newbies to the language will read your code, this prevents some confusion.

Fixed Code: (I don't know if this will come out properly indented on the forum but here goes)

CODE
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{
     //setup
     int choice;
     vector<string> games;

     cout << "1. List Games\n";
     cout << "2. Add Games\n";
     cout << "3. Remove Games\n";
     cout << "4. Quit\n";

     cout << "Choose an option: ";
     cin >> choice;

     while(choice == 1 || choice == 2 || choice == 3)
     {
         switch(choice)
         {
             case 1:
                  
             for(int i = 0; i < games.size(); i++)
             {
                 cout << games[i] << endl;
                 cout << "Choose an option: ";
                 cin >> choice;
             }
             break;
                
             case 2:
             {// brace added here
                 cout << "Enter game to add: ";
                 string game;
                 cin >> game;
                 games.push_back(game);
                 cout << "New List:\n";
            
                 for(int i = 0; i < games.size(); i++)
                 {
                     cout << games[i] << endl;
                     cout << "Choose an option: ";
                     cin >> choice;
                 }
             }// brace added here
             break;
            
             case 3:
             {// brace added here
                 cout << "List:\n";
                 for(int i = 0; i < games.size(); i++)
                 {
                     cout << (i + ". " + games[i]) << endl;
                     cout << "Enter number of game to remove: ";
                     int remove;
                     cin >> remove;
                     games.erase((games.begin() + remove)); // Parenthesis added after "remove"
                     cout << "New List:\n";
                
                     for(int i = 0; i < games.size(); i++)
                     {
                         cout << games[i] << endl;
                         cout << "Choose an option: ";
                         cin >> choice;
                     }
                 }
             }// brace added here
             break;
         }
     }
     //shutdown
     cout << "Thank you for using chapter 4, exercise 1!";
     return 0;
}


Lastly, good job on creating your first program without instructions from a book or tutorial, keep up the good work biggrin.gif

Regards, Shoxin.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.