2016-05-24 38 views
-4

Mein cin >> OneDecision; wird übersprungen, und dann beendet es irgendwie das ganze Programm dort.Mein cin >> wird übersprungen/ignoriert

Ich versuchte cin.getline(OneDecision, 40), aber egal was. Es wird nicht funktionieren, und ich weiß nicht warum nicht. Und ich kann das Spiel nicht wirklich fortsetzen, wenn ich es nicht behebe.

Dies ist für eine Klassenaufgabe, es wird zu einem kurzen textbasierten Horror-Spiel. Wenn ich Hilfe bekommen könnte, wäre das nett. Ich bin ein Amateur, hab Erbarmen.

#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

Wenn alle Ihre Eingaben Räume haben, dann müssen Sie 'verwenden getline' auf irgendwelche dieser Eingänge. – NathanOliver

+1

Und hier ist die Antwort für Ihr nächstes Problem, das entstehen wird, nachdem Sie anfangen, 'getline' zu verwenden: http://StackOverflow.com/a/21567292/3410396 –

+1

'option ==" run to window "' funktioniert nur wenn 'option' ist eine' std :: string'. Bei einem C-Style-Char-Array müssen Sie 'strcmp' verwenden. –

Antwort

2

Antwort

Sie müssen sich ändern:

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

zu:

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

Oder besser "std :: string" an erster Stelle verwenden – Slava