![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 94
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4
![]() |
Re: Newb interactive fiction
>> std::exit(0);
exit() is not in std namespace, so remove std:: because it will cause a compiler error.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#4 |
|
Not a user?
Join Date: Sep 2007
Posts: 245
Rep Power: 1
![]() |
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Re: Newb interactive fiction
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 |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Re: Newb interactive fiction
It is according to cstdlib. Not stdlib.h though.
|
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 94
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#8 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 499
Rep Power: 4
![]() |
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
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#9 |
|
Not a user?
Join Date: Sep 2007
Posts: 245
Rep Power: 1
![]() |
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.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Nov 2005
Location: Estonia
Posts: 94
Rep Power: 0
![]() |
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 ¤tRoom;
}
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |