程序在未识别 scanf 的情况下终止
Program is terminating without recognizing scanf
我不知道为什么我的程序在调用 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();
}
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;
}
先生,您是否检查过语法不足("%d",&variable);
不是。不足(“%d”,变量);
问题是scanf("%c", &ans);
扫描了前一个scanf
留下的换行符。
修复很简单。只需在 confirmStats
中的两个 scanf
中的 %c
之前添加一个 space。 space 是一个 whitespace 字符,scanf
格式字符串中的 whitespace 字符告诉 scanf
扫描任意数量的 whitespace 字符,如果有的话,直到第一个非白色 space 字符。
改进代码:
#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;
}
我不知道为什么我的程序在调用 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();
}
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;
}
先生,您是否检查过语法不足("%d",&variable); 不是。不足(“%d”,变量);
问题是scanf("%c", &ans);
扫描了前一个scanf
留下的换行符。
修复很简单。只需在 confirmStats
中的两个 scanf
中的 %c
之前添加一个 space。 space 是一个 whitespace 字符,scanf
格式字符串中的 whitespace 字符告诉 scanf
扫描任意数量的 whitespace 字符,如果有的话,直到第一个非白色 space 字符。
改进代码:
#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;
}