我的 cin>> 被跳过/忽略

My cin>> is being skipped/ignored

本文关键字:gt 忽略 我的 cin      更新时间:2023-10-16

我的cin >> OneDecision;不断被跳过,然后它只是在那里结束了整个程序。

我试过cin.getline(OneDecision, 40),但无论如何。它行不通,我不知道为什么不行。除非我修复它,否则我无法继续游戏。

这是针对课堂作业的,它将是一个简短的基于文本的恐怖游戏。如果我能得到一些帮助,那就太好了。我是业余爱好者,怜悯。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iomanip>
#include <limits>

using namespace std;
int main()
{
    char player1 [40],
    char fear [40];
    char friend1 [40];
    char friend2 [40];
    int start;
    char option [40], option2 [40], option3 [40], option4 [40];
    char OneDecision [40], OneDecision2 [40];
    cout << "Hey.... what's your name?n";
    cin.get(player1, 40);
    cout<< "Well, " << player1 << ". What is your biggest fear?n";
    cin >> fear ;
    cout <<"Name one friend.n";
    cin >> friend1;
    cout << "Name another friend... or make one up if you have none.n";
    cin >> friend2;
    cout<< "Are you ready to play?n 1. Yes or 2. No.n";
    cin >> start ;
    if (start == 1)
       {
       cout << "You'll later regret it...n You have entered HORROR HOUSE.";
       }
    else if (start == 2)
       {
        cout << "I don't blame you. Why the hell would you want to enter a    horror house to be honest...nnnn";
        cout << "                                      END GAME.";
       }

     Sleep(2500);
     system("cls");

     cout<< "You, " << friend1 << " and " << friend2 << " are staying at the mansion " << friend1 << "'s family recently nbought.";
     cout << "It's night, and you are in your room sleeping.nWhen you here a loud noise, a thump.nYou get out of bed, and look around to see if something fell.n";
     cout << "The door you though you closed was left open, and but other than that nothing is wrong.nnnnn";
     Sleep (12000);
     cout << "               But then you hear a horrible scream from outside.nn";
     cout << "                Run to window and see? Or Run to " << friend1 << "'s room?n";
     cin.get(option, 40);
     cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // The program terminates at this line, I've tried other ways of saying for it to ignore the next line but none of them worked, this was the last thing I tried. 

     if (string(option) == "run to window" || string(option) == "window")
     {
       cout << "      n  You run to the window and see " << friend1 << " laying dead on the ground outside.n";
       cout << "               Go downstairs and out the house toward" << friend1 << "?n                Or run to " << friend2 << "'s room to wake them up." ;
       cin.get(OneDecision, 40); 
     if (OneDecision == "go downstairs")
     {
      cout << "You are running downstairs. You run out of the house, and    search you're backyard for" << friend1 << ", but , you can't find their body.";
     }
     return 0;

答案

您需要更改:

if (option == "run to window" || "window")

自:

if (string(option) == "run to window" || string(option) == "window")