Here is my assignment:
Ask the user to input their name.
If their name ends with a vowel, ask them to input their age as well.
If their age is even, print
Wow, you're special!. Otherwise, if their age is odd, ask them to input their birth year.If their birth year is even, print
Oh, you're still special!. Otherwise, printYou will be special next year..If their name ends with a consonant though, print
You're awesome!
Here is my code:
#include <stdio.h>
#include <string.h>
int main() {
// variables
char name[100];
int age, birth;
printf("Enter name: ");
scanf("%[^\n]s", &name);
// formula for getting last letter of the name
int size = strlen(name);
int last = size - 1;
if (name[last] == 'a' || name[last] == 'e' || name[last] == 'i' ||
name[last] == 'o' || name[last] == 'u') {
printf("Enter age: ");
scanf("%d", &age);
if (age %2 == 0) {
printf("Wow, you're special!\n");
} else {
printf("Enter birth year: ");
scanf("%d", &birth);
}
if (birth % 2 != 0) {
printf("You will be special next year.\n");
} else {
printf("Oh, you're still special!\n");
}
} else {
printf("You're awesome!\n");
} // end of nested if
return 0;
}
The problem is with the birth variable: the age and birth variables seems to be connected, because when the condition of age executes the condition of birth executes as well leading to a multiple executions of conditions.