Reputation: 3
I know this is a really silly piece of code and it's only for me to try and get my head around things... I think I've done okay, (apart from not properly indenting)
But I'm struggling with giving the user the option to quit the program using the 'Q' Character, but can't seem to get it right?
Can anybody please help..
#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;
}
Upvotes: 0
Views: 230
Reputation: 16540
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
Upvotes: 0
Reputation: 23015
For that, don't use:
scanf("%d", &yearBorn)
use instead:
char someString[100];
scanf("%99s", someString)
and then check if someString
is "Q" (using stricmp
). If it is not, then you can use atoi
to convert it to a number.
Upvotes: 1
Reputation: 11428
Just declare an opt
variable of type char
and add this just before the } while (years>1);
line:
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;
To avoid executing always the last printf
, enclose it in an if
statement
if (opt!=quit)
{
age = YEAR-yearBorn;
printf("You're old enough.. for heavens sake you are %d years old \n", age);
}
Upvotes: 1