Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with basic C++ question

Started by Basse85 Jun 23, 2008 at 4:47 PM 29 replies 4k views
Original Post
Basse85
Basse85
Hi. Im trying to learn CPP through developing very basic games.. Im writing a text based game called Traitor, were the player is in a room with a couple of people and through dialog must find the traitor. Im trying to write a input loop that only quits if command == /"quit" But the loop ends any way. Any ideas? source:

 
   string command;
    
    do
    {
           cin >> command;

           if (command == "/help")
              help();

           if (command == "look")
              look();

           if (command == "talk")
              talk();

                              
    }while (command == "/quit");

swiftcoder
swiftcoder
Surely that should be while (command != "/quit");?
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
nightech
nightech
swiftcoder got it in one, right now, what you're doing is only keeping the game active if the player types /quit. so change your conditional at the end to while (command != "/quit"); (notice that is 'not equals' "!=", instead of 'is equal to' "==") and that should solve your problem :)
-------------------------------------All i know is i don't know anything
Basse85
Basse85
Thanks for the help swiftcoder and nightech. But some how the problem remains..
Basse85
Basse85
Thanks but the problem remains.
Here's the source.
I'm a bit embarresd for some of the code, like all the system("PAUSE");
But i'll post it anyway.

#include <iostream>#include <string>#include <vector>using namespace std;//func declarationstring behavior();string look();string help();int main(){    vector<string> people;    people.push_back("Ned");    people.push_back("Earl");    people.push_back("Jenna");        string command;        cout << "\tWelcome to Traitor";    cout << "\nType '/help' for game instructions\n";            while (command != "/quit")    {          cin >> command;          if (command == "/help")             help();          if (command == "look")             look();    }                    system("PAUSE");    return 0;    }//Functionsstring behavior() //people behavior{       cout << "Hmmm...\n";       system("PAUSE");       return 0;}string look() //Inspekt the room{    cout << "Its a small room with big paintings on the wall.\n";    cout << "There's an warm fire at the fireplace in the right side of the room.\n";    cout << "Infront of the fireplace lies a carpet made out of a dead bear\n";    cout << "On your left is a small bar\n";    system("PAUSE");    return 0;}string help(){       cout << "\n\tcommands:";       cout << "\nsystem commands\n";       cout << "/help -command list\n";       cout << "/quit -quit game\n";       cout << "\ngame commands\n";       cout << "look -room overview\n";       cout << "talk -talk to persons in the room\n";              system("PAUSE");       return 0;}


Were can the problem be?

Any thougths on the design and choices I have made is welcome.
P.S Dont know pointers and classes yet..
nightech
nightech
EDIT: As SiCrane pointed out, this was useless as it's the wrong library entirely, ignore this suggestion

Here's the MSDN link for the string information in .net
http://msdn.microsoft.com/en-us/library/system.string.equals(VS.71).aspx

you could use while(!(command->equals("/quit"))) and that looks like it will take care of it. Just keep in mind that will be the same for all of your strings, the == operator just doesn't love strings.

Hope that helps and fixes it up for you :)

[Edited by - nightech on June 24, 2008 11:39:18 AM]
-------------------------------------All i know is i don't know anything
Basse85
Basse85
um..
base operand of ->' has non-pointer type std::string'

Im quite stupid, what does .NET mean? Anything else in the code I have to change?

Im reading beginning cpp game programming (half way in) and thats were my cpp knowledge comes from so I'm a very very basic "coder"

Thanks for all your answers and your patience. cpp can be quite overwhelming..
nightech
nightech
Quote:
Original post by SiCrane
Quote:
Original post by nightech
Here's the MSDN link for the string information in .net
http://msdn.microsoft.com/en-us/library/system.string.equals(VS.71).aspx


std::string != System.String.


I'm guessing in that case that a good link would be
http://www.cprogramming.com/tutorial/string.html which is for the std::string version instead which does indeed support the == operator, forgive my assumption.

Since it looks like you're using Visual C++, have you tried tracing the execution to see what it does through the loop, why it drops at the end? you might even try just putting some cout statements for each command. to see where it drops out.

Just a few suggestions, hope they help :)
-------------------------------------All i know is i don't know anything
nightech
nightech
Quote:
Original post by Basse85
um..
base operand of ->' has non-pointer type std::string'

Im quite stupid, what does .NET mean? Anything else in the code I have to change?

Im reading beginning cpp game programming (half way in) and thats were my cpp knowledge comes from so I'm a very very basic "coder"

Thanks for all your answers and your patience. cpp can be quite overwhelming..


Ignore that post, SiCrane pointed out that i was very wrong with what library i specified there, that one will not work.
-------------------------------------All i know is i don't know anything
Basse85
Basse85
Im using Dev cpp that came with the book but will switch to Visual C++ as soon as im done with the book (want all the example code to compile)

Tanks for the link. Reading it right now =)
yaustar
yaustar
string help(){       cout << "\n\tcommands:";       cout << "\nsystem commands\n";       cout << "/help -command list\n";       cout << "/quit -quit game\n";       cout << "\ngame commands\n";       cout << "look -room overview\n";       cout << "talk -talk to persons in the room\n";              system("PAUSE");       return 0;}

In all your functions except for main, the function declaration is saying that it will return a string object, so why are you returning an int?
Steven Yau
[Blog] [Portfolio]
Eskapade
Eskapade
Your functions' return values are strings, but they return an integer (return 0;). That's like writing "string foo = 0" and your compiler would complain about that. You will learn a way how to track this bug down later (try catch mechanism), don't worry about it yet.
Basse85
Basse85
yaustar and Eskapade, you solved the problem!
Thanks to you all for taking time to help me =)
Eskapade
Eskapade
Onto another point: you won't want to use "cin >> command" in this case. So to save you from some funny bugs, use this instead:
string command;while (true){    cout << "> " << flush;    getline(cin, command);    // quit app (breaks the while loop so code after it still gets executed)    // this could be cleaning up allocated memory or just sayin good bye ;)    if (command == "quit")        break;    // show help    else if (command == "help")        help();    // look around the room    else if (command == "look")        look();    // Je ne parle francais?    else        cout << "Pardon?" << endl;}


I added the cout << "> " << flush here to illustrate something. When your program starts you'll have this neat prompt where you can input something, e.g. "> help" then hit enter. This will work with your code. However, type "help me" or "help quit" and you'll get multiple prompts/answers, because cin always extracts single words. The getline(cin, command) function will not do this, but look at the input as a whole.
DevFred
DevFred
Your functions behavior, look and help have a return type string, yet you return 0 which is not a string. If you don't want to return anything replace the return type with void and get rid of the return 0;
void behavior() //people behavior{       cout << "Hmmm...\n";       system("PAUSE");}
Basse85
Basse85
Yea I noticed that bug. Thanks allot =)
DevFred
DevFred
Whoops, looks like I was 20 minutes late. That's what happens when you open multiple topics simultanously and answer one by one without refreshing first :)
Basse85
Basse85
Im using a vector to store the curent people in the room.
Any suggestions on a way to give each person different conversations and new unlockable topics to talk about?

I want it to run like this:

> talk
TALK TO: >Ned
Ned: Whats up?
> (different topics to choose from)
K-
K-
Quote:
Original post by Basse85
Any suggestions on a way to give each person different conversations and new unlockable topics to talk about?


What have you come up with so far?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.