2014-09-12 71 views
-1

問題是我想'o',不要重複...我試圖找到,但我失敗了! 也解釋我是如何發生這種情況。我是一個新的學習程序員!C++中的數組錯誤

#include <iostream> 
#include <string> 
#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <time.h> 

using namespace std; 

int main() 
{ 
    //all in-game variables here 
    string o = "o"; 
    int oH = 0; 
    int oW = 0; 
    //variables ending point... 

    bool run = true; 
    bool frameShow = true; 
    char input; 
    int width = 53; 
    int height = 22; 

    string px[width][height]; 

    while (run) 
    { 
     for (int xEmp=0; xEmp<height; xEmp++){ 
      for (int yEmp=0; yEmp<width; yEmp++) 
      { 
       px[xEmp][yEmp]= " "; 
      } 

      if (frameShow) 
      { 
       clrscr();   // Must be at start 

       px[oH][oW] = o; 

       for (int x=0; x<height; x++) 
       { 
        for (int y=0; y<width; y++) 
        { 
         cout << px[x][y]; 
        } 
        cout << "\n"; 

        frameShow = false; // Must be at end 
       } 
      } 

      if (kbhit()) 
      { 
       input = getch(); 

       // Most Used ones are: 
       // char 119 for "w" 
       // char 97 for "a" 
       // char 115 for "s" 
       // char 100 for "d" 
       // char 32 for "space" 
       // char 27 for ESC 

       if (input == 119) 
       { 
        oH--; 
        frameShow = true; 
       } 
       else if (input == 115) 
       { 
        oH++; 
        frameShow = true; 
       } 
       else if (input == 97) 
       { 
        oW--; 
        frameShow = true; 
       } 
       else if (input == 100) 
       { 
        oW++; 
        frameShow = true; 
       } 
       else if (input == 27) 
       { 
        // Game exits... 

        // To terminate, use: 
        run = false; 
       } 

       if(oH > height) 
       { 
        oH = height; 
       } 
       else if(oH < 0) 
       { 
        oH = 0; 
       } 

       if(oW > width - 1) 
       { 
        oW = width - 1; 
       } 
       else if(oW < 0) 
       { 
        oW = 0; 
       } 

      } 
     } 
    } 

    // Output for confiming program termination 
    clrscr(); 
    cout << "\n - Terminated! - \n"; 
    return 0; 
} 
+6

請正確格式化您的代碼 - 目前幾乎不可讀。 – 2014-09-12 13:19:07

+0

好的!但是,請幫助我,然後... – user3820248 2014-09-12 13:20:06

+8

不。不,不,不,不。刻錄代碼(鹽土),解僱你的C++老師(或者教程作者),拿起一本現代書籍並重新開始。 – 2014-09-12 13:22:19

回答

1

作爲開始,widthheight使用正被改變。 px[width][height];,px[xEmp][yEmp],xEmp<height,yEmp<width。有一次,您使用它作爲px[width][height];,然後px[height][width];。保持你的代碼一致!

另外if(oH > height) { oH = height; }是錯誤的。從高度減去一個。

這也可能沒有你想要什麼:

for (int x=0; x<height; x++) 
    for (int y=0; y<width; y++) 
    { 
     cout << px[x][y]; 
    } 
    cout << "\n"; 

使用括號正確,如果你不知道如何使用它們,把他們永遠幸福!

再次,我覺得你沒有正確使用括號:

for (int xEmp=0; xEmp<height; xEmp++){ 
    for (int yEmp=0; yEmp<width; yEmp++) 
    { 
     px[xEmp][yEmp]= " "; 
    } 
... // do other things 
} 

我想你想關閉它馬上設置應有盡有回太空和他們做其他工作。就像現在一樣,它會將一行設置回空格,打印所有內容並運行您的代碼,並且僅在空白的下一行之後。

for (int xEmp=0; xEmp<height; xEmp++){ 
    for (int yEmp=0; yEmp<width; yEmp++) 
    { 
     px[xEmp][yEmp]= " "; 
    } 
} 
... // do other things 

PS:clrscr()是一個非標準的功能,只能在Windows中,我認爲,對於Linux使用system('clr');

Ps2的:你爲什麼要使用std::string代替字符,如果你只存儲字符?

+0

中仍然有錯誤的括號哇!它修復了所有......非常感謝你!我的親戚批評我,你的密碼只不過是垃圾,我非常沮喪!現在,看起來t.pimentel的答案喚醒了我......簡單的錯誤有時很難找到,因爲我正在思考超過1小時!這裏的課程是「保持秩序!」。謝謝! – user3820248 2014-09-12 13:52:42

+0

沒問題。爲了使調試更容易,嘗試在這裏和那裏打印一些變量來分析發生了什麼。 Ps:請將答案設爲正確。 Ps2:Upvote它。 Ps3:嘗試使用更好的變量名稱'px'並不代表任何東西,也許''gameMap',而不是'o',也許'oSymbol'。 – 2014-09-12 13:57:41

+0

是啊!你是對的!實際上,我使用名爲C4droid(andriod OS)的應用程序在手機中進行編程。53和22個字符以正確的方式完全填滿了我的屏幕,當我們按'w'或's'時,看起來好像o以正確的方式向下移動...對於其他操作系統,只需在每一行之後添加一個新行53個字符。這就是爲什麼它看起來很糟糕。另外,我該如何做這件事?是的,我想在每個「框架」後面準確地製作「空間」,以便不會獲得更多的金額。正如我之前提到的,我沒有使用Linux。 Ps2答案:用戶還必須在其中的一部分輸入名稱(未來) – user3820248 2014-09-12 14:02:34