Sign in to follow this  

Help with basic C++ question

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

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");

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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 declaration
string 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;

}



//Functions
string 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..

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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");
}

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
I'm pretty new to C++ myself, but aren't people typing /quit not "/quit"?

In that case wouldn't it be

if (command != quit)

since command is already defined as a string?

Share this post


Link to post
Share on other sites
Quote:
In that case wouldn't it be

if (command != quit)

I fail to see what issue you are raising. He compares command to /quit (command != "/quit"). Where is the variable quit defined?

Share this post


Link to post
Share on other sites
if you're using std::strings you would want to do


std::string command = "not quitting";
while(command.compare("/quit" != 0) // Compare returns 0 if the strings are equal
{
std::cin >> command; // I would use getline(std::cin, command) personally

if(command.compare("/help") == 0)
{
help();
}
if(command.compare("look") == 0)
{
look();
}
if(command.compare("talk") == 0)
{
talk();
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by rofseek
if you're using std::strings you would want to do


Why do you need to use compare?

Share this post


Link to post
Share on other sites
You have to use compare because this isn't java. "/quit" is a character pointer to the compile time constant C string "/quit" it may or may not evaluate as equal to the std::string "/quit".

Share this post


Link to post
Share on other sites

This topic is 3459 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this