2015-10-20 90 views
-1

我不知道爲什麼我的程序在調用confirmStats();之前終止。我在main()中包含了相關的所有內容,以防在我的程序中出現其他問題。程序在不識別scanf的情況下終止

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

int str, intel, charis; 
int totalPts =5; 

/* Returns a number. 
    User must input an integer. 
*/ 
int getNumber(int number){ 
    while(scanf("%d", &number) != 1){ 
    printf("You did not enter a valid number\n"); 
    scanf("%*s"); 
    } 
    return number; 
} 
/* Individual stat points */ 
int stat(int number){ 
    number = getNumber(number); 
    while(number > totalPts){ 
    printf("You only have %d stat points left\n", totalPts); 
    printf("Enter a number less than or equal to %d:\t", totalPts); 
    number = getNumber(number); 
    } 
    totalPts -= number; 
    printf("Points remaining:\t%d\n", totalPts); 
    return number; 
} 


/* Player stat points */ 
void getStats(){ 
    printf("You're alotted %d stat points to spend in Strength, Intellect, and Charisma\n", totalPts); 

    printf("Intellect:\t"); 
    intel = stat(intel); 

    printf("Strength:\t"); 
    str = stat(str); 

    printf("Charisma:\t"); 
    charis = stat(charis); 

    printf("\nIntellect: %d\t Strength: %d\t Charisma: %d\n", intel, str, charis); 

} 

void confirmStats(){ 
    char ans; 
    scanf("%c", &ans); 
    while(ans == 'n'){ 
    str = 0; 
    intel = 0; 
    charis = 0; 
    getStats(); 
    printf("Are these correct?:\ty/n: "); 
    scanf("%c", &ans); 
    } 
} 
void main(){ 

    printf("\nSafe choice..."); 
    printf("\n"); 
    printf("Alright, how old are you?\n"); 
// int age, str, intel, charis; 
    int age; 
// int totalPts = 5; 
    age = getNumber(age); 
    getStats(); 
    printf("Are these correct? "); 
    printf("\n"); 
    printf("y/n:\t"); 
    printf("\n"); 
    confirmStats(); 




} 
+0

好了,你有一些的printf的和一個調試器。哪個呼叫失敗? –

+1

爲什麼你將'age'傳遞給'getNumber()?它未初始化... –

+0

謝謝,user2121013。修正它 – FatFockFrank

回答

0

問題是scanf("%c", &ans);會掃描前一個scanf遺留的換行符。

修復很簡單。只需在%c之前在confirmStats的兩個scanf之間加一個空格。格式字符串scanf中的空格是一個空格字符和一個空白字符,它告訴scanf掃描任意數量的空白字符(如果有),直到第一個非空白字符。

改進代碼:

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

int str, intel, charis; 
int totalPts = 5; 

/* 
    Returns a number. 
    User must input an integer. 
*/ 
int getNumber(){ 
    int number; 
    while(scanf("%d", &number) != 1){ 
    printf("You did not enter a valid number\n"); 
    scanf("%*s"); 
    } 
    return number; 
} 

/* Individual stat points */ 
int stat(){ 
    int number; 

    number = getNumber(); 

    while(number > totalPts){ 
    printf("You only have %d stat points left\n", totalPts); 
    printf("Enter a number less than or equal to %d:\t", totalPts); 
    number = getNumber(); 
    } 

    totalPts -= number; 
    printf("Points remaining:\t%d\n", totalPts); 

    return number; 
} 


/* Player stat points */ 
void getStats(){ 
    printf("You're alotted %d stat points to spend in Strength, Intellect, and Charisma\n", totalPts); 

    printf("Intellect:\t"); 
    intel = stat(); 

    printf("Strength:\t"); 
    str = stat(); 

    printf("Charisma:\t"); 
    charis = stat(); 

    printf("\nIntellect: %d\t Strength: %d\t Charisma: %d\n", intel, str, charis); 

} 

void confirmStats(){ 
    char ans; 
    scanf(" %c", &ans); 
    while(ans == 'n'){ 
    str = 0; 
    intel = 0; 
    charis = 0; 
    getStats(); 
    printf("Are these correct?:\ty/n: "); 
    scanf(" %c", &ans); 
    } 
} 
int main(void){ 
    printf("\nSafe choice..."); 
    printf("\n"); 
    printf("Alright, how old are you?\n"); 
    age = getNumber(); 

    getStats(); 

    printf("Are these correct? "); 
    printf("\n"); 
    printf("y/n:\t"); 
    printf("\n"); 
    confirmStats(); 

    return 0; 
} 
0
int getNumber(int number){ 
    int ok = 0; 
    while (!ok){ 
    if(scanf("%d", &number) != 1){ 
     printf("You did not enter a valid number\n"); 
     while(getchar()!='\n'); 
    } 
    else { 
     getchar(); 
     ok=1; 
    } 
    } 
    return number; 
} 
-1

爵士,做u已經檢查了語法是很少( 「%d」,&變量); 不是。很少( 「%d」,變量);

相關問題