Archived

This topic is now archived and is closed to further replies.

A rather confusing problem this time...

This topic is 5886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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!]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"?)

Share this post


Link to post
Share on other sites
quote:
Original post by Fractal
please someone answer this as I can''t find the solution anywhere else!


  1. Desperation doesn''t help

  2. Some things you have to solve for yourself. Here''s a hint: read in the entire line of input and parse it for the valid tokens. How do you tokenize? string has find_first_of(), find_first_not_of(), find_last_of() and find_last_not_of() methods; you can take it from there.

Share this post


Link to post
Share on other sites