Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2008, 4:31 PM   #1
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
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?
commodore is offline   Reply With Quote
Old Mar 16th, 2008, 4:51 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Old Mar 17th, 2008, 12:08 AM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Newb interactive fiction

>> std::exit(0);
exit() is not in std namespace, so remove std:: because it will cause a compiler error.
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Mar 17th, 2008, 12:33 AM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Mar 17th, 2008, 1:24 AM   #5
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 343
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
Re: Newb interactive fiction

Quote:
Originally Posted by Jabo View Post
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.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Mar 17th, 2008, 1:53 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Newb interactive fiction

Quote:
Originally Posted by Ancient Dragon View Post
>> 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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 17th, 2008, 5:36 AM   #7
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
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?
commodore is offline   Reply With Quote
Old Mar 17th, 2008, 6:39 AM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
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
__________________
<<Freelance Programmer>> << Hobby Site>>
Ancient Dragon is offline   Reply With Quote
Old Mar 17th, 2008, 7:45 AM   #9
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Mar 17th, 2008, 8:41 AM   #10
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
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;
}
commodore is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
RE working in the Interactive but not in script nytrokiss Python 11 Nov 20th, 2006 9:37 PM
C++ programming newb INeedAGig1210 C++ 2 May 28th, 2006 12:47 PM
newb needs help with classes and char arrays AbalahDoon C++ 13 Jul 2nd, 2005 12:01 PM
Interactive of program? jesslee Visual Basic .NET 0 Jun 18th, 2005 3:29 AM
Newb here Komodo C++ 16 Jun 2nd, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:04 AM.

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