程序终止而不识别scanf

时间:2015-10-20 23:12:23

标签: c scanf terminate

我不知道为什么我的程序会在调用confirmStats();之前终止。我在main()中包含了所有相关内容,以防问题出现在我的程序中的其他位置。

#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();




}

3 个答案:

答案 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 :(得分:0)

问题在于scanf("%c", &ans);会扫描上一个scanf留下的换行符。

修复很简单。只需在%c中的两个scanfconfirmStats之前添加一个空格即可。空格是一个空格字符,格式字符串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;   
}

答案 2 :(得分:-1)

先生,您是否检查过语法不足(“%d”,&amp; variable); 不。很少( “%d”,变量);