Jump to content
  • Advertisement
Sign in to follow this  
ozzoright

Help with small C++ program

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

I've been reading this book called "Beginning C++ Game Programming" and at the end of each chapter there are some exercises. Well for once I decided I might as well do one and see how they work for me and after finishing one I compiled it (I'm using the Dev C++ complier). The problem is, right after you type in a game and enter the do loop to add another game, it crashes. I'm a real novice when it comes to programming and so I have no idea what happened. I'm sure it's really obvious so any help would be appreciated.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> list;
    string games;
    char remove = 'remove';
    string y = games;
    
    vector<string>::iterator cycle;
    
    cout << "Welcome to Game List, a program which allows the user to list   
             his/her favorite \ngames. Type 'list' ";
    cout << "to see your list of games, type 'remove' and 'name of game' to     
             delete that game and 'quit' to quit. ";
    
    cout << "\n\nWhat is your favorite game? ";
    cin >> games;
           
    do
    {
         cout << "\nAny other games you like? ";
         cin >> games;
         
         if (games == "list")
         cout << "\n\nYour list is: ";
         for (cycle = list.begin(); cycle != list.end(); ++cycle)
         cout << *cycle << endl;
         
         if (games == "remove"); (list[0].erase());
         
    } while (y == games);
    
    if (games == "quit");
        cout << "Press the enter key to exit.";
    
    cin.ignore (cin.rdbuf()->in_avail() + 2);
    
    return 0;
}


[Edited by - ozzoright on August 4, 2005 6:45:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
Well there are a couple problems with this.

First what Compiler are you using?

I'm surprised that this line even compiles.

char remove = 'remove';



these lines:

         
if (games == "list")
cout << "\n\nYour list is: ";
for (cycle = list.begin(); cycle != list.end(); ++cycle)
cout << *cycle << endl;





Look at your if statement. All it will do is execute the cout statement.

It will always execute the for loop. You would need to enclose both the for loop and cout statements in brackets.

Like:


if (games == "list")
{
cout << "\n\nYour list is: ";
for (cycle = list.begin(); cycle != list.end(); ++cycle)
cout << *cycle << endl;
}





But now look at the for loop.

You set the iterator to the beginning. But then Immediately skip to the next one when you preincrement ++cycle. You would want to post increment like cycle++.

Since you are always executing this for loop, the first time it jumps straight to the end, cycle is pointing badly then. That is why you are crashing.

Next look at the condition for your while statement.

while(y==games)

when does y ever get updated. If your program made it to this statement, what would happen?

Below and outside of your loop is the line:


if (games == "quit");
cout << "Press the enter key to exit.";





Shouldn't this be inside your loop? Wouldn't it need to do something like to trigger the exit from the loop.

Now loo even close at the line

if (games == "quit");

what do you think that semi colon will do?

Lots of mistakes. But Mistakes are good. Thats how you learn.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
OK, a fair few things wrong with this, and I'll point you in the directions you need to go.

1. You have two IF statements and you've placed a ; at the end which means you always execute the statements that you believe should be conditional.

e.g.

if (games == "remove");
(list[0].erase());

2. you never add anything to the vector, and when you hit the above line tries to delete the 0th entry in an empty vector.

3. your loop never triggers as y == games is always false (so you'll always drop out).

4. The quit line should be in the loop.

5. the "char remove = 'remove';" is a problem also, as a ' normally is for a single character association, if you want a string of characters, use a char pointer (i.e. char * remove = "remove";).

Keep going, your doing very well, and you'll get this going, and feel very good when you do.

Post back if you need anything else.

Share this post


Link to post
Share on other sites
Quote:
Original post by HAM
Lots of mistakes. But Mistakes are good. Thats how you learn.


Very true indeed. This may disappoint you, but persevere.

I've been at C++ seriously for about 7 months now, and I'm still making basic mistakes left right and centre.

But I've learned from them, and getting better as I go.

Good luck,

ukdeveloper.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!