我知道这是一段非常愚蠢的代码,只有我才能尝试解决问题...我认为我做得还不错,(除了没有正确缩进)
但是我努力让用户可以选择使用' Q'退出程序。性格,但似乎不能正确吗?
任何人都可以帮忙..
#include <stdio.h>
#define YEAR 2015
int main()
{
int yearBorn, age, ageLimit,years;
char quit = 'Q';
ageLimit=16;
do // The program will continue running until the user enters an age greater than 16 or presses the 'Q' character
// to quit the program
{
printf("\nPlease Enter The Year You Were Born: ");
scanf(" %d", &yearBorn);
age= YEAR-yearBorn;
years=ageLimit-age;
if (yearBorn==YEAR) //This IF statement will run if the user enters the current year!
{
printf("Please double check your entry as it seems ");
printf("that you havent been born yet?");
}
else if (age<=ageLimit) //If the users age is less than 16 program prints the following messages
{
printf("\nYou are too young to play the Lottery!\n");
printf("you have to wait %d year%s before you can play!\n\n",years,(years!=1) ? "s" : " ");
printf("You are %d\n",age);
}
}while (years>=1);
age = YEAR-yearBorn;
printf("You're old enough.. for heavens sake you are %d years old \n", age);
return 0;
}
答案 0 :(得分:1)
为此,请勿使用:
scanf("%d", &yearBorn)
改为使用:
char someString[100];
scanf("%99s", someString)
然后检查someString
是否为&#34; Q&#34; (使用stricmp
)。如果不是,则可以使用atoi
将其转换为数字。
答案 1 :(得分:1)
只需声明类型为opt
的{{1}}变量,然后在char
行之前添加:
} while (years>1);
为避免始终执行上一个printf ("Press %c and ENTER to quit, or just ENTER to repeat: ", quit);
do
opt = getchar();
while (opt!=quit && opt!='\n');
if (opt==quit)
break;
,请将其括在printf
语句中
if
答案 2 :(得分:0)
the following is one way to handle this application
notice the inclusion of error checking
notice the inclusion of a proper user prompt
notice the method for checking of the user entered 'Q'
notice the method for exiting the application
notice that 'age', once calculated does not need to be recalculated
when exiting program
this algorithm can fail when the year born is 16 years ago
as it would then depend on the birth month/day compared to today
It would be a good idea to check the converted yearBorn for validity
as a atoi() failure would result in yearBorn being 0;
#include <stdio.h>
#include <stdlib.h> // exit and EXIT_FAILURE
#define YEAR (2015)
#define AGE_LIMIT (16)
#define QUIT 'Q'
int main()
{
int yearBorn;
int age;
int years = 1;
char buffer[20];
do // The program will continue running until the user enters an age greater than 16 or presses the 'Q' character
// to quit the program
{
printf("\nPlease Enter The Year You Were Born or 'Q' to quit: ");
if( NULL == fgets( buffer, sizeof buffer, stdin ) )
{ // then fgets failed
perror( "fgets for birth year failed" );
exit( EXIT_FAILURE );
}
// implied else, fgets successful
if( QUIT == buffer[0] ) {break;}
yearBorn = atoi(buffer);
age= YEAR-yearBorn;
years=AGE_LIMIT-age;
if (yearBorn==YEAR) //This IF statement will run if the user enters the current year!
{
printf("Please double check your entry as it seems ");
printf("that you havent been born yet?");
}
else if (age<AGE_LIMIT) //If the users age is less than 16 program prints the following messages
{
printf("\nYou are too young to play the Lottery!\n");
printf("you have to wait %d year%s before you can play!\n\n",years,(years!=1) ? "s" : " ");
printf("You are %d\n",age);
} // end if
}while (years>=1);
if( years <= 0 )
{
printf("You're old enough.. for heavens sake you are %d years old \n", age);
}
return 0;
} // end function: main