text based game

hi i am trying to make a tex based game in C++.
the way i was planning to do this was using the following sort of method:

#include <iostream>
#include <string>
using namespace std;

int lastCommand1;
int lastCommand2;
int currentLocation("startingroom");


int main ()
{
cout << endl;
cin >> lastCommand1 >> lastCommand2;

switch (currentLocation)
{
case "startingroom":
switch (lastCommand1)
{
case 'look':
switch (lastCommand2)
{
case 'room':
cout >> "You are in a small starting room, there is nothing you can do here yet.";
break;
}
}
}
}

but you I get over 30 error messages when I try to do this and they are:

c:\documents and settings\daniel\my documents\visual studio 2008\projects\game stuff\game stuff\main.cpp(24) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
c:\program files\microsoft visual studio 9.0\vc\include\string(424) : see declaration of 'std::operator >>'

over and over again...
if someone could help me I would be very thankful

PS. I know this is messy but I tried to include as much info as possible
in the above example if someone typed:

look room

they would get:

You are in a small starting room, there is nothing you can do here yet.
i also tried this in IF ELSE commands, didnt work either...same error message.
try splitting the command
cin << lastcommand1 << lastcommand2;
in
cin << lastcommand1;
cin << lastcommand2;

cin won't take 2 arguments
You cannot use a switch with Strings.
Switch only works with countable arguments.

[code}
switch(some_variable)
{
case 1: do_this;
break;

case 2: do_that;
break;
}
[/code]
int main
Last edited on
is that the same with IF ELSE? how else can the game work???
also, cin can take two arguments, try:

int main ()
{
cout << endl;
cin >> lastCommand1 >> lastCommand2;
cout << lastCommand1 << lastCommand2;
}
IVE WORKED IT OUT!!!!
I got over 30 error messages because the cout command on line 21 was followed by >> instead of <<.
Heh heh heh, your first foray into the wonders of C syntactical flexibilities.
Now you know why C and C++ programmers are so rigid about stuff --we have to be. Welcome to the club!

BTW, I've been meaning to respond sooner, but you might want to consider how you organize your data. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
typedef enum { north, south, east, west } exits_t;
struct room_t
  {
  string name;
  string description;
  int exits[ sizeof( exits_t ) ];  // index of room in indicated direction
  };

room_t rooms[] = {
  { "Entry hall", "This room is filled with snakes.", { -1,  1,  2, -1 } },
  { "South room", "This room is cold.",               {  0, -1,  2, -1 } },
  { "East room",  "This room is fragrant.",           { -1,  1, -1,  0 } }
  };


Things like 'room' are multi-byte character constants, and not strings. It is unlikely that the user will know exactly which integer to enter for it. It is better to translate a string to an enum or other integer constant that can be used in a switch.

You cannot initialize an integer with a string (int foo("bogon");).

Finally, I suggest that you use getline() do do all user input. If you need to convert it to something else, do the conversion with a stringstream instead of using >> with cin directly.

It might be worth your while to spend a little time perusing the tutorial at this site. It really is top-notch.
http://www.cplusplus.com/doc/tutorial/

Hope this helps.
Last edited on
Topic archived. No new replies allowed.