A rather confusing problem this time...
I am in the process of making a text-based game in DOS, where the player inputs commands such as "north", "spell", and so on.
But one very annoying bug presents itself when i run the game.
If two words are typed after "What will you do?" that are both unrecognisable commands, then "That''s not a valid command" will appear twice instead of once. And if one of the words is not recognised but the other is, then "That''s not a valid command" will apear once and the programme will then perform the function linked to the recognisable word.
ROOM2:
Room.nRoom = 2;
system("Cls");
cout << "You can go north, south, east or west.\n";
ROOM2WHAT:
cout << "What will you do?\n";
cin >> Room.sRoom2;
else if (Room.sRoom2 == "north") {goto ROOM11;}
else if (Room.sRoom2 == "south") {goto ROOM1;}
else if (Room.sRoom2 == "east") {goto ROOM7;}
else if (Room.sRoom2 == "west") {goto ROOM3;}
else {cout << "\nThat''s not a valid option\n";}
goto ROOM2WHAT;
I type in "go north" and the programme tells me i''ve typed in an invalid word and it then goes to ROOM11.
Where on earth have I gone wrong here?
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
October 29, 2001 12:55 PM
cin seperates input at all white space characters. White space is returns, tabs, and of course, spaces.
So in other words, cin will return everything up until the first space, then next time it is called, it will return everything after that space and until the next, and so forth.
--Drakonite
So in other words, cin will return everything up until the first space, then next time it is called, it will return everything after that space and until the next, and so forth.
--Drakonite
October 29, 2001 01:17 PM
instead of using cin, use getline.
getline(variable, # of characters to search until);
The first input is the variable you want to put the info into, like the direction that the character is moving. The second input is the number of characters to move through until it finds a ''\n'' (newline) character. Newlines are created when someone hits the enter key. Just use a large number like 256 for the second variable.
So, in other words,
getline(Room.sroom2, 256);
its that easy.
getline(variable, # of characters to search until);
The first input is the variable you want to put the info into, like the direction that the character is moving. The second input is the number of characters to move through until it finds a ''\n'' (newline) character. Newlines are created when someone hits the enter key. Just use a large number like 256 for the second variable.
So, in other words,
getline(Room.sroom2, 256);
its that easy.
My compiler tells me that getline() requires 3 arguments, not 2, what''s the third one?
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
Is there any difference between getline() and gets() ?
Seems like they both do the same thing.
Seems like they both do the same thing.
The road may be long, wind may be rough. But with a will at heart, all shall begone. ~savage chant
quote: Original post by savagerx
Is there any difference between getline() and gets() ?
Seems like they both do the same thing.
They basically do. The gets function is part of the standard C i/o library. I wouldn''t use gets though, I would use fgets with stdin (safer that way).
[Resist Windows XP''s Invasive Production Activation Technology!]
The third argument is what the ending character is. By default, it is when the user presses enter (\n), but you can set it to any other character. So cin.getline() reads till the user hits that character, such as \n or \t.
Oh no, my bad: I forgot to mention that Room.sRoom1 is a string, not a char, and when I compile the code, it tells me that it
"cannot convert parameter 1 from ''class std::basic_string,class std::allocator >'' to ''char *''"
Could I be any dumber?
(What do you mean, "No"?)
"cannot convert parameter 1 from ''class std::basic_string,class std::allocator >'' to ''char *''"
Could I be any dumber?
(What do you mean, "No"?)
Could I be any dumber?(What do you mean, "No"?)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement