2013-06-23 46 views
0

我的程序編譯正確,但我有問題,當我運行它。第一次scanf(寬度)工作正常,但當我嘗試與另一個scanf(身高)我得到分段錯誤11. 我可以做這個程序工作沒有使用指針。 (另外我需要限制檢查功能,因爲我必須在我的程序中一次又一次地使用它)。檢查用戶輸入之間的一定限制,如果不再詢問,直到輸入正確的輸入

#include <stdio.h> 
void limitChecker(int x, int y, int* input); 
int main(void) 
{ 
    int* x; 
    int* y; 
    printf("Enter the width of the windows. (3 - 5) : "); 
    scanf("%d", x); 
    limitChecker(3, 5, x); 
    printf("width: %d \n", *x); 
    printf("Enter the height of the windows. (2 - 4) : "); 
    scanf("%d", y); 
    limitChecker(2, 4, y); 
    printf("Height: %d \n", *y); 

} 

void limitChecker(int x, int y, int* input) 
{ 
    while(!(*input>=x && *input<=y)) 
    { 
    printf("Please enter a value between (%d - %d): ",x,y); 
    scanf("%d", input); 
    } 
} 

回答

0

你需要使用()在scanf函數中使用的變量的引用。

例如,scanf("%d", &x);

scanf()第一個參數是對於數據的類型,和下面的參數(一個或多個)是指針到您想要存儲的用戶輸入的列表。

更正後的代碼:

#include <stdio.h> 
void limitChecker(int x, int y, int* input); 
int main(void) 
{ 
    int x; 
    int y; 
    printf("Enter the width of the windows. (3 - 5) : "); 
    scanf("%d", &x); 
    limitChecker(3, 5, &x); 
    printf("width: %d \n", x); 
    printf("Enter the height of the windows. (2 - 4) : "); 
    scanf("%d", &y); 
    limitChecker(2, 4, &y); 
    printf("Height: %d \n", y); 

} 

void limitChecker(int x, int y, int* input) 
{ 
    while(!(*input>=x && *input<=y)) 
    { 
    printf("Please enter a value between (%d - %d): ",x,y); 
    scanf("%d", input); 
    } 
} 
+0

我不能因爲它的指針(如果我添加&我在編譯期間出錯)這是我的錯誤「格式'%d'期望類型'int *',但參數2類型'int **'」 – Nakib

+0

好吧,我看到。當你實際聲明它們是'int x,y'時,你只需創建'x'和'y'指針。 – Zak

+0

你知道他們是變數。我遇到了指針輸入的問題(你會感到困惑,因爲我在函數中使用了相同的名稱x和x以及主函數) – Nakib

0

你沒有分配內存來存放xy

將它們分配在堆棧上,然後使用運算符的&地址獲取指向該內存的指針。

#include <stdio.h> 
int limitChecker(int x, int y, int input); 
int main(void) 
{ 
    int x; 
    int y; 
    printf("Enter the width of the windows. (3 - 5) : "); 
    scanf("%d", &x); 
    x = limitChecker(3, 5, x); 
    printf("width: %d \n", x); 
    printf("Enter the height of the windows. (2 - 4) : "); 
    scanf("%d", &y); 
    y = limitChecker(2, 4, y); 
    printf("Height: %d \n", y); 

} 

int limitChecker(int x, int y, int input) 
{ 
    while(!(input>=x && input<=y)) 
    { 
    printf("Please enter a value between (%d - %d): ",x,y); 
    scanf("%d", &input); 
    } 

    return input; 
} 

如果你想xy爲指針,那麼你必須在使用之前爲它們分配有效的內存。

int * x = malloc(sizeof(int)); 
int * y = malloc(sizeof(int)); 
+0

我能做到同樣的事情,而無需使用指針 – Nakib

+0

@Nakib然後你需要分配內存來保存您的實際'int'。默認情況下,int *不帶有效內存來保存內容。編輯。 –

+0

我沒有得到你。我是說無指針不帶指針 – Nakib

0
#include <stdio.h> 

int limitChecker(int x, int y, int value){ 
    return x <= value && value <= y; 
} 

int inputInt(void){ 
    //x >= 0 
    int x = 0; 
    int ch; 
    while('\n'!=(ch=getchar())){ 
     if('0'<=ch && ch <= '9') 
      x = x * 10 + (ch - '0'); 
     else 
      break; 
    } 
    return x; 
} 

int main(void){ 
    int x, y; 
    do{ 
     printf("Enter the width of the windows. (3 - 5) : "); 
     x = inputInt(); 
    }while(!limitChecker(3, 5, x)); 
    printf("width: %d \n", x); 
    do{ 
     printf("Enter the height of the windows. (2 - 4) : "); 
     y = inputInt(); 
    }while(!limitChecker(2, 4, y)); 
    printf("Height: %d \n", y); 
    return 0; 
} 
相關問題