2016-05-24 12 views
-4

私のcin >> OneDecision;はスキップされ続けます。My cin >>がスキップ/無視されています

私は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...\n\n\n\n"; 
     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.\n\n\n\n\n"; 

    Sleep (12000); 

    cout << "    But then you hear a horrible scream from outside.\n\n"; 
    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; 
+2

ご入力のいずれかがスペースを持っている、あなたはgetl​​ine'上の '使用する必要がある場合それらの入力のいずれか。 – NathanOliver

+1

そして、あなたの次の問題の答えは、あなたが 'getline'を使い始めた後に起こります:http://stackoverflow.com/a/21567292/3410396 –

+1

'option ==" run to window "は'option'は' std :: string'です。 Cスタイルのchar配列では 'strcmp'を使う必要があります。 –

答えて

2

回答

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

へ:

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

または、最初に 'std :: string'を使うのが良い – Slava

関連する問題