2017-09-26 15 views
0

我有一個簡單的軟件用於在控制檯中打印e電路板。我使用addBoard()(用於添加組件的命名方法很差)運行下面的程序,它只是不打印任何內容。即使它是序列中的最後一個。如果我將它註釋掉,程序可以正常工作,並在Visual Studio代碼終端窗口中打印所有內容。添加或刪除方法是順序最後呈現C程序無用

這可能是由於什麼原因造成的? (還有任何關於我使用指針hehe的指針......或者其他任何不是最佳實踐的指針都會非常感謝。)謝謝!

#include <stdio.h> 
#include <stdlib.h> 

char *posArr[10][30]; 

void printInstructions(){ 
    printf("Hello and welcome! Here you can configure your own breadboard\n"); 
    printf("To do this you will use coordinates using this syntax:\n"); 
    printf("width: 3\n"); 
    printf("height: 5\n"); 
    printf("After that a component will be choosen.\n"); 
    printf("It would look like below:\n");  
} 

void printBoard(){ 
    for (int i = 1; i <= 10; i++) //Height 
    { 
     printf("\n"); 
     for (int j = 1; j <= 30; j++) //Width 
     { 
      printf("%s", posArr[i][j]); 
     } 
    } 
} 

void clearBoard(){ 
    for (int i = 1; i <= 10; i++) //Height 
    { 
     for (int j = 1; j <= 30; j++) //Width 
     { 
      posArr[i][j] = ". "; 
     } 
    } 
} 

void buildBoard(int *width, int *height){ 
    //*height -= 1; 
    //*width -= 1; 
    for (int i = 1; i <= 10; i++) //Height 
    { 
     //printf("\n"); 
     for (int j = 1; j <= 30; j++) //Width 
     { 
      if (*height == i && *width == j){ 
       posArr[i][j] = "¤ "; 
       //printf("¤ "); 
      } 
      else { 
       posArr[i][j] = ". "; 
       //printf(". "); 
      } 
     } 
    } 
} 

void addBoard(){ 
    int h, w; 

    while(1) 
    { 
     printf("Width: "); 
     scanf("%d", w); 
     printf("Height: "); 
     scanf("%d", h); 

     buildBoard(&w,&h); 

     //Add length later 
     printBoard(); 
    } 
} 

int main() { 
    printInstructions(); 
    int a=3, b=5; 
    buildBoard(&a,&b); 
    printBoard(); 
    clearBoard(); 
    //addBoard(); 
} 
+3

您沒有正確索引數組,從'1'到超出範圍。例如'for(int i = 1; i <= 10; i ++)'應該是'for(int i = 0; i <10; i ++)' –

+3

您還正在傳遞'scanf'函數'int'而不是' INT *'。 – user694733

+1

這是開始使用調試器的絕好機會。您可以逐步瀏覽代碼並隨時觀察變量。 – user694733

回答

0

可以,而不是從`0`試試這個代碼(稍微修改代碼)

#include <stdio.h> 
#include <stdlib.h> 

char *posArr[10][30]; 

void printInstructions(){ 
    printf("Hello and welcome! Here you can configure your own breadboard\n"); 
    printf("To do this you will use coordinates using this syntax:\n"); 
    printf("width: 3\n"); 
    printf("height: 5\n"); 
    printf("After that a component will be choosen.\n"); 
    printf("It would look like below:\n"); 
} 

void printBoard(){ 
    for (int i = 0; i < 10; i++) 
    { 
     printf("\n"); 
     for (int j = 0; j < 30; j++) 
     { 
      printf("%s", posArr[i][j]); 
     } 
    } 
} 

void clearBoard(){ 
    for (int i = 0; i < 10; i++) 
    { 
     for (int j = 0; j < 30; j++) 
     { 
      posArr[i][j] = ". "; 
     } 
    } 
} 

void buildBoard(int *width, int *height){ 
    for (int i = 0; i < 10; i++) 
    { 
     for (int j = 0; j < 30; j++) 
     { 
      if (*height == i && *width == j){ 
       posArr[i][j] = "¤ "; 
      } 
      else { 
       posArr[i][j] = ". "; 
      } 
     } 
    } 
} 

int main() { 
    printInstructions(); 
    int a=3, b=5; 
    buildBoard(&a,&b); 
    printBoard(); 
    clearBoard(); 
} 
+1

OP的代碼的一部分在這裏丟失 – ryyker

+0

addBoard()函數沒有在問題代碼中使用,所以我刪除了該函數。運行良好,你可以在線編譯運行它呃在這個鏈接「http://rextester.com/l/cpp_online_compiler_visual」 – Linkon

+1

你應該解釋你做了什麼來糾正代碼 – purplepsycho

相關問題