Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Newb interactive fiction (http://www.programmingforums.org/showthread.php?t=15428)

commodore Mar 16th, 2008 3:31 PM

Newb interactive fiction
 
I'm trying to learn C++ and I started coding an interactive fiction game for practice and I can't get this to compile:
:

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
 
void handle_input(std::string input)
{
    if (input=="quit")
    {
        std::cout<<"Goodbye";
        std::exit(0);
    }
 
    else if (input=="look"){std::cout<<currentRoom.description;}
 
    else {std::cout<<"I don't understand what you're saying\n";}
}
 
class Room
{
    public:
        std::string description;
        std::vector<Room> exits;
};
 
int main()
{
    Room firstRoom;
    firstRoom.description="Description of first room";
    Room currentRoom=firstRoom;
 
    std::cout<<"Hi"<<"\n";
    std::string input;
 
    while (true)
    {
        std::cout<<">>> ";
        std::cin>>input;
        handle_input(input);
    }
    return 0;
}

What am I doing wrong and what am I doing bad?

OpenLoop Mar 16th, 2008 3:51 PM

Re: Newb interactive fiction
 
I can tell you have a poor understanding of variable definition scopes. To sum it up, when you define a variable inside a function (like currentRoom in main()), the variable is called Local (meaning that it is only defined inside that function). If you try to access it from another function, you will get a compiler error. If you have a need of that variable in a different function, you can pass it as an argument (just like you did with input).
Another problem I see is that Class Room is only defined AFTER the handle_input() function. This will create problems for you if you try to define a variable of type Room inside of that function. To get around this, you can either move the definition of Class Room and put it before handle_input(), or put a class prototype.

Ancient Dragon Mar 16th, 2008 11:08 PM

Re: Newb interactive fiction
 
>> std::exit(0);
exit() is not in std namespace, so remove std:: because it will cause a compiler error.

Jabo Mar 16th, 2008 11:33 PM

Re: Newb interactive fiction
 
using namespace std;

If you'll use the line in red after your preprocessor directives (#include statements), you won't have to type std:: before your statements.

mrynit Mar 17th, 2008 12:24 AM

Re: Newb interactive fiction
 
Quote:

Originally Posted by Jabo (Post 142592)
using namespace std;

If you'll use the line in red after your preprocessor directives (#include statements), you won't have to type std:: before your statements.

I see alot of books and tutorials start with this before namespaces are mentioned.

Ooble Mar 17th, 2008 12:53 AM

Re: Newb interactive fiction
 
Quote:

Originally Posted by Ancient Dragon (Post 142590)
>> std::exit(0);
exit() is not in std namespace, so remove std:: because it will cause a compiler error.

It is according to cstdlib. Not stdlib.h though.

commodore Mar 17th, 2008 4:36 AM

Re: Newb interactive fiction
 
Thanks for your help people. I coded python before and in python it seemed that only classes and objects had namespaces.

Is it a rule that you should usually define classes before anything else?

People on ##c++@freenode said I shouldn't use "using namespace std" so I thought I'd trust them.

If I do currentRoom=firstRoom then currentRoom is a copy of firstRoom? Should I use a pointer/reference instead?

Ancient Dragon Mar 17th, 2008 5:39 AM

Re: Newb interactive fiction
 
>>Is it a rule that you should usually define classes before anything else?
Classes must be defined before they can be used or referenced. So the standard way to do it is to put class definitions in a header file so that the header file can be included in all of the *.cpp files that use it.

>>People on ##c++@freenode said I shouldn't use "using namespace std" so I thought I'd trust them
That is a generally accepted way to do it. Most programmers don't like the "using namespace std" because it brings everything from std namespace into the program. In very short programs it doesn't really matter so I use that too because it reduces typing std:: in front of everything. But in larger programs you will want to do something different, such as putting this after the includes:
:

using std::cout;
using std::cin;
using std::endl;
// etc


Jabo Mar 17th, 2008 6:45 AM

Re: Newb interactive fiction
 
I've seen some examples on msdn using the class header file this way also. If the header is name header.h then they add the line using namespace header.h and then they don't have to type their class name before class methods. I'm not sure how smart that is really, maybe somebody with more experience could shed some light.

commodore Mar 17th, 2008 7:41 AM

Re: Newb interactive fiction
 
I tried to take it further but it's now even more f*d up. I have trouble understanding this pointer stuff.
:

#include <iostream>
#include <string>
#include <map>
#include <cstdlib>

class Room
{
    public:
        std::string description;
        std::map<std::string, Room*> exits;
};

Room handle_input(std::string input, Room currentRoom)
{
    if (input=="quit" || input=="exit")
    {
        std::cout<<"Goodbye";
        std::exit(0);
    }
   
    else if (input=="n")
    {
        if (currentRoom.exits.find("n")!=currentRoom.exits.end())
        {
            currentRoom=currentRoom.exits["n"];
            std::cout<<currentRoom.description<<std::endl;
            return &currentRoom;
        }
        else {std::cout<<"There's no exit to north"<<std::endl;}
    }

    else if (input=="look"){std::cout<<currentRoom.description<<std::endl;}
   
    else {std::cout<<"I don't understand what you're saying"<<std::endl;}
}

int main()
{
    Room firstRoom;
    firstRoom.description="Description of first room";
    Room* currentRoom=&firstRoom;
   
    Room secondRoom;
    secondRoom.description="You're in the second room";
    firstRoom.exits["n"]=&secondRoom;
   
    std::cout<<"Hi"<<"\n";
    std::string input;

    while (true)
    {
        std::cout<<">>> ";
        std::cin>>input;
        currentRoom=handle_input(input, *currentRoom);
    }
    return 0;
}



All times are GMT -5. The time now is 9:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC