2016-07-15 49 views
-6

這是用於cs50馬里奧金字塔問題集。我讓它在沒有功能的情況下工作,但我想嘗試一些不同的東西。由於用戶輸入數字後的更改符合參數,程序將停止。它不會打印「#」,或者像過去那樣的「」。C#,函數運行後我的程序停止運行,我做錯了什麼?

我在網上學習這門課程,任何幫助將不勝感激。

#include <stdio.h> 
#include <cs50.h> 

int towerheight(void); 

int main(void) 
{ 
    printf("how high would you like the tower? "); 
    int height = towerheight(); 
    int pound; 
    int space; 
    for (int i = 0; i < height; i++) 
    { 
     for (space = height - i; space >= 0; space--) 
     { 
      printf(" "); 
     } 
     for (pound = 0; pound < i + 2; pound++) 
     { 
      printf("#"); 
     } 
     printf("\n"); 
    } 
} 
int towerheight(void) 
{ 
    int num; 
    do 
    { 
     printf("Your number must be between 1 - 23: "); 
     num = GetInt(); 
    } 
    while (num <= 0 || num >= 24); 
    return 0; 
} 
+5

這不是C#。 – crashmstr

+0

函數返回應該是'return num'而不是'return 0' –

+0

學習如何調試,而不是在SO上發佈最小的問題。如果你在調試器中檢查代碼,你會發現'height'爲零,所以你的'for'循環甚至不會運行,因爲它沒有使用'i Quantic

回答

3

的問題是在你的函數,取代的最後一行:

int towerheight(void) 
{ 
    int num; 
    do 
    { 
     printf("Your number must be between 1 - 23: "); 
     num = GetInt(); 
    } 
    while (num <= 0 || num >= 24); 
    return num; // replace by this 
} 

你總是返回0for迴路不會滿足的條件,因爲height總是0

+0

他在使用之前進行初始化,do會先運行一次,在使用之前對其進行初始化。 – Dispersia

+0

是的,你是對的。我很抱歉,我編輯了我的答案 –

相關問題