Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Mar 17th, 2008, 8:43 AM   #11
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Newb interactive fiction

Here's a corrected version. class Room is passed both directions as a pointer.
Room* handle_input(std::string input, Room* currentRoom)
{
    if (input=="quit" || input=="exit")
    {
        std::cout<<"Goodbye";
        std::exit(0);
    }
    
    else if (input=="n")
    {
        std::map<std::string, Room*>::iterator it;
        if ( (it = currentRoom->exits.find("n"))!=currentRoom->exits.end())
        {
            currentRoom = it->second;
            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;}
    return 0;
}

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;
}
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 17th, 2008, 9:29 AM   #12
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 94
Rep Power: 0 commodore is an unknown quantity at this point
Re: Newb interactive fiction

Thanks for helping. Is -> used with pointers? There's still a problem. When I run it, type in "look" and then "n" it crashes.

EDIT: it's because handle_input returns 0 after "look". I replaced it with return currentRoom;
commodore is offline   Reply With Quote
Old Mar 17th, 2008, 10:01 AM   #13
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Newb interactive fiction

0 is the correct return value because look is not in the map. The calling function needs to handle that situation where the value pass to input_value() is not found.
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote
Old Mar 17th, 2008, 11:43 AM   #14
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

maybe you should forget about pointers for now and stick to copies. Once your program is operational, save a backup somewhere and start converting it to pointers.

Edit: never mind. I didn't read Dragon's posts before posting.
OpenLoop 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 8:37 PM
C++ programming newb INeedAGig1210 C++ 2 May 28th, 2006 11:47 AM
newb needs help with classes and char arrays AbalahDoon C++ 13 Jul 2nd, 2005 11:01 AM
Interactive of program? jesslee Visual Basic .NET 0 Jun 18th, 2005 2:29 AM
Newb here Komodo C++ 16 Jun 2nd, 2005 1:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:25 PM.

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