0

I am currently working on a text based adventure game as a project for class. I have mostly everything started and working fine. The only problem is when I ask the user which room they want to change to, if they enter a blank input, then a message should output saying "You must choose a room." For the life of me I cannot figure it out. Any help is much appreciated.

Code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

bool game_play = true;
bool game_start = true;
int room_change;
int room_current = 0;

while (game_play == true) {
    if (game_start == true) {
        srand((unsigned int)time(NULL));
        room_change = rand() % 2 + 1;
        game_start = false;
    }
    else {
        for (bool check = false; check == false;) { // Check if input is invalid
            cin >> room_change;
            if (cin.fail()) {
                cout << "Choose an existing room.";
                cin.clear();
                cin.ignore();
            }
            else if (room_change == room_current) {
                cout << "You're already in that room.";
            }
            else {
                check = true;
            }
        }
    }
    switch (room_change) {
    case 1:
        cout << "You are in room 1.";
        room_current = 1;
        break;
    case 2:
        cout << "You are in room 2.";
        room_current = 2;
        break;
    case 3:
        game_play = false;
        break;
    default:
        cout << "That room doesn't exist.";
    }
}
return 0;
}
Jbro
  • 269
  • 2
  • 8
  • 2
    don't call `srand` in a loop [srand() — why call it only once?](https://stackoverflow.com/q/7343833/995714) – phuclv Mar 03 '19 at 08:20

3 Answers3

2

I just ran your code and when you hit enter, it will keep waiting until you enter a number or something invalid such as a character or a string. I did find that if you change your code from

cin >> room_change;

to

cin >> noskipws >> room_change;

when the user inputs a blank, it will cause the cin.fail() to return true and then proceed to print "Choose an existing room."

In your situation, the while loop will keep getting called until we have valid input. The "Choose an existing room" does get repeated because room_change is an integer, so when we hit enter, the '\n' will be left in the buffer. The while loop on the next iteration then reads that '\n' and executes the cin.fail() before letting you input something else. One solution I found is to use more cin.ignore() statements.

for (bool check = false; check == false;) { // Check if input is invalid
    cin >> noskipws >> room_change;
    if (cin.fail()) {
        cout << "Choose an existing room.";
        cin.clear();
        cin.ignore();
    } else if (room_change == room_current) {
        cout << "You're already in that room.";
        cin.ignore();
    } else {
        check = true;
        cin.ignore();
    }
}

The reason is because we want to get rid of that '\n' so that the cin.fail() does not execute. However, I did find that when you input a character, it will print "Choose an existing room" twice. It will print the first time because a character is not an integer, and a second time because of that '\n'.

A. Rey
  • 61
  • 3
  • Damn, even I didn't know about this. – Sayem Chaklader Gearspec Mar 03 '19 at 08:23
  • It's a good idea, however the issue I have when I use this is that even with numeric inputs it still triggers the cin.fail(). – Jbro Mar 03 '19 at 09:07
  • In your scenario, because room_change is an integer, I assume the '\n' will remain in the buffer, so the cin.fail() gets called to read in that '\n'. The fix to that will be to add more cin.ignore() statements. I will edit the code for you to see. – A. Rey Mar 03 '19 at 10:24
0

The only problem is when I ask the user which room they want to change to, if they enter a blank input, then a message should output saying "You must choose a room."

Using std::getline and then extracting the number from the line using a std::istringstream is a better strategy for that.

std::string line;
std::cout << "Choose an existing room. ";
while ( std::getline(std::cin, line) )
{
   // Try to get the room_change using istringstream.
   std::istringstream str(line);
   if ( str >> room_change )
   {
      // Successfully read the room.
      break;
   }

   // Problem reading room_change.
   // Try again.
   std::cout << "Choose an existing room. ";
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0
#include <iostream>

using namespace std;

int main(){
    int room_change=200;

    cout<<"Enter Blank";
    cin>>room_change;

    if(room_change==NULL){
       cout<<"There is NO-THING"<<endl; 
    }

    if(room_change!=NULL){
       cout<<"There is something and that is :"<<room_change<<endl; 
    }


return 0;   
}

But a much simpler approach to this would be to use Strings. If this is a Homework of sort and you are limited to Integer variable only. Its much more complicated if you want to detect if an Buffer is empty or not. Regardless of homework limitation, the OS layer input is String based. How can I use cin.get() to detect an empty user input?

  • 1
    If you think `room_change==NULL` will evaluate to `true` if the user enters a blank line of input, then you are mistaken. Initialize `room_change` to an unusual number, say 200, and try it. – R Sahu Mar 03 '19 at 08:28