2016-11-30 24 views
0
#include "stdafx.h" 
#include <string>; 
#include <iostream>; 
#define newline '\n'; 
using namespace std; 




string initial; 
string a; 
string b; 
string c; 
string object; 
string sword; 
string testswing; 

int main() 
{ 

    cout << "you awake in a forest with no memory what do you do?"; 
    cout << newline; 
    cin >> initial; 
    if (initial == "look left" || initial == "look forward" || initial == "look behind"){ 
     cout << newline; 
     cout << "you see a dense forest"; 
     cout << newline; 

    } 

    else (initial == "look right"); { 
     cout << newline; 
     cout << "you see a opening with a shape in the distance"; 
     cout << newline; 
    } 

    cin >> a; 
    cout << newline; 
    if (a == "go right") 
    { 
     cout << newline; 
     cout << "you see a trap lucily you dident set it off. might want to SEARCH AREA before going into open areas"; 
     cout << newline; 
     cin >> c; 
     cout << newline; 
     if (c == "go forward") 
     { 
      cout << newline; 
      cout << "you arrive at the strange object do you want to INSPECT OBJECT?"; 
      cout << newline; 
      if (object == "inspect object") 
      { 
       cout << newline; 
       cout << "You see the object is a sword stuck in a stone"; 
       cout << newline; 
       cin >> sword; 
       if (sword == "pull sword"); 
       { 
        cout << newline; 
        cout << "the sword breaks free of the stone you have obtained the SHORT SWORD"; 
         cout << newline; 
        cout << "use this item when appropriate by using the command SWING SWORD"; 
        if (testswing == "swing sword"); 
        { 
         cout << newline; 
         cout << "you swing the sword it hits the rock and breaks"; 
         cout << newline; 
         cout << "we told you only to use the sword when appropriate now look what you've done your adventure is over already"; 
          cout << newline; 
          system("pause"); 
          return 0; 
        } 
       } 
      } 
      else 
      { 
       cout << newline; 
       cout << "the object draws you twards it seemingly by magic"; 
       cout << newline; 
      } 
     } 
     else (c == "go left" || c == "go right"|| c == "go back"); 
     { 
      cout << "there is nothing but empty feilds for miles you get lost and die"; 
      cout << newline; 
      system("pause"); 
       return 0; 
     } 
    } 
    else { 
     cout << "you cant go that way"; 
    } 

} 

程序在第一個用戶輸入後退出,不管輸入是什麼,如果它的外觀離開,向前看,後面看,或看起來正確程序退出後,我無法找到問題。它說用代碼0(0X0)退出程序我找不到第一個用戶輸入後程序退出的原因

+1

當你使用調試器,通過這個代碼一行在一個時間步驟,你做了什麼意見,就其執行流程? –

+1

'else(initial ==「look right」);'這在語法上是正確的,在語義上是無意義的! '否則,如果(初始==「看起來正確」)'可能是你在做什麼。之後你也會有類似的錯誤。 –

+0

解決這些問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

回答

1

代碼的cin>>initial部分只需要在用戶輸入直到第一空空間

所以,當你進入LOOK留在初始字符串只包含目光就是爲什麼你的代碼沒有輸入IF語句。

嘗試函數getline(初始),而不是CIN >>

退房這個link

0

我發現這個計劃的一些問題。

  1. 如果你想要多個單詞輸入 - 使用getline(cin,str) - cin將只取第一個單詞作爲輸入。

  2. 一旦發出EOF(或任何其他錯誤)信號,所有嘗試輸入數據失敗。在嘗試進一步輸入之前,您必須首先清除流(cin.clear();)。所以對於每個cin,使用cin.clear()來刷新輸入緩衝區。

在你的程序

現在,如果你沒有使用明確(),然後輸入多個單詞輸入,比第一個字將被輸入到第一個字符串,該輸入的第二個字將成爲第二個「CIN輸入>>一個「,因此它不會停在那裏,因爲它已經得到了輸入。

試試這個: -

//#include "stdafx.h" 
#include <string>; 
#include <iostream>; 
#define newline '\n'; 
using namespace std; 

string initial; 
string a; 
string b; 
string c; 
string object; 
string sword; 
string testswing; 

int main() 
{ 

    cout << "you awake in a forest with no memory what do you do?"; 
    cout << newline; 
    getline(cin, initial); 
    if (initial == "look left" || initial == "look forward" || initial == "look behind"){ 
     cout << newline; 
     cout << "you see a dense forest"; 
     cout << newline; 

    } 

    else if(initial == "look right") { 
     cout << newline; 
     cout << "you see a opening with a shape in the distance"; 
     cout << newline; 
    } 
    cin.clear(); 
    getline(cin, a); 
    cout << newline; 
    if (a == "go right") 
    { 
     cout << newline; 
     cout << "you see a trap lucily you dident set it off. might want to SEARCH AREA before going into open areas"; 
     cout << newline; 
     cin >> c; 
     cout << newline; 
     if (c == "go forward") 
     { 
      cout << newline; 
      cout << "you arrive at the strange object do you want to INSPECT OBJECT?"; 
      cout << newline; 
      if (object == "inspect object") 
      { 
       cout << newline; 
       cout << "You see the object is a sword stuck in a stone"; 
       cout << newline; 
       cin >> sword; 
       if (sword == "pull sword"); 
       { 
        cout << newline; 
        cout << "the sword breaks free of the stone you have obtained the SHORT SWORD"; 
         cout << newline; 
        cout << "use this item when appropriate by using the command SWING SWORD"; 
        if (testswing == "swing sword"); 
        { 
         cout << newline; 
         cout << "you swing the sword it hits the rock and breaks"; 
         cout << newline; 
         cout << "we told you only to use the sword when appropriate now look what you've done your adventure is over already"; 
          cout << newline; 
          system("pause"); 
          return 0; 
        } 
       } 
      } 
      else 
      { 
       cout << newline; 
       cout << "the object draws you twards it seemingly by magic"; 
       cout << newline; 
      } 
     } 
     else (c == "go left" || c == "go right"|| c == "go back"); 
     { 
      cout << "there is nothing but empty feilds for miles you get lost and die"; 
      cout << newline; 
      system("pause"); 
       return 0; 
     } 
    } 
    else { 
     cout << "you cant go that way"; 
    } 
    cout<<endl; 
    system("PAUSE"); 
} 
相關問題