【问题标题】:Program is terminating without recognizing scanf程序在未识别 scanf 的情况下终止
【发布时间】:2015-10-21 19:54:39
【问题描述】:

我不知道为什么我的程序在调用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();




}

【问题讨论】:

  • 嗯,你有一些 printf 和一个调试器。哪个通话失败了?
  • 你为什么将 'age' 传递给 'getNumber() ?它未初始化...
  • 谢谢,user2121013。解决了它
  • 编译所有警告和调试信息(例如gcc -Wall -Wextra -g),然后使用调试器gdb)。您的“修复我的代码”问题在这里是题外话。

标签: c scanf terminate


【解决方案1】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-31
        • 1970-01-01
        相关资源
        最近更新 更多