Jump to content


Photo

Learning c++


  • Please log in to reply
3 replies to this topic

#1 npsken

npsken

    Young Padawan

  • Members
  • Pip
  • 26 posts
  • Gender:Male
  • Interests:Computer Programming<br />Graphic Design

Posted 20 January 2008 - 01:02 AM

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:

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:
#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:
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.

Edited by npsken, 20 January 2008 - 01:03 AM.


#2 Encrypt

Encrypt

    Young Padawan

  • Members
  • Pip
  • 9 posts
  • Gender:Male
  • Location:Northern Ireland

Posted 17 February 2008 - 02:52 AM

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:

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:

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)

#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 :)

Regards, Shoxin.

Edited by Shoxin, 17 February 2008 - 03:04 AM.


#3 blyind

blyind

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 25 April 2010 - 12:33 PM

i am also reading this book, i had problems with this one aswell, but finally got it right.

i figured id post my code so others that are in the same situation can see a couple different ways of doing this project.

//game list
 //a favorite games list utility end of chapter exercise
 
 #include <iostream>
 #include <string>
 #include <vector>
 #include <algorithm>
 #include <ctime>
 #include <cctype>
 
 using namespace std;
 
 int main()
 {
	 vector<string> favGames; //collection of favorite games
	 vector<string>::const_iterator pointer;
	 vector<string>::iterator pointer2;
	 string gameEntry;
	 
	 cout << "favorite game listing utility\n";
	 
	 //main loop begins here
	 
	 while(gameEntry != "exit")
	 {
	   cout << "enter 'add' to add a game, 'view' to view the list, 'remove' to remove a title or 'exit' to quit\n";
	   cin >> gameEntry;
	   if(gameEntry == "view")
	   {
		 cout << "\n\nFavorite Games List :\n";
		   for(pointer = favGames.begin(); pointer != favGames.end(); ++pointer)
		   {			 
			   cout << *pointer << endl;	
		   }		 
	   }
	   if(gameEntry == "add")
	   {
					cout << "\nEnter the game name to be added\n";
		  cin >> gameEntry;
		  favGames.push_back(gameEntry);			 
	   }
	   if(gameEntry == "remove")
	   {
		   cout << "\nenter the name of the game to be removed from list\n";
		   cin >> gameEntry;
		   pointer2 = find(favGames.begin(), favGames.end(), gameEntry);
		   favGames.erase(pointer2);					
	   }
	 }
  cout << "\nthanks for useing my game listing app\n\n";
  system("pause");
  return 0;
 }


#4 _*aprillove20_*

_*aprillove20_*
  • Guests

Posted 21 October 2010 - 04:01 PM

Relevant tutorial and code, it very helpful information.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users