[C programming, done on visual studios] I\'m supposed to make a program where yo
ID: 3675107 • Letter: #
Question
[C programming, done on visual studios]I'm supposed to make a program where you input age, weight, birth month and if it is correct, it will output a congratulations for being correct. Otherwise, if it is not correct, it will put a "sorry, your guess was not correct!". Age is 25, weight is 128, birth month is April.
The name and weight is working correctly, but the month is always wrong. Even putting in 'April' will still yield an incorrect output.
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
float age;
float weight;
char birthMonth;
printf("Enter your guess for age: ");
scanf_s("%f", &age);
printf("Enter your guess for weight: ");
scanf_s("%f", &weight);
printf("Enter your guess for birth month: ");
scanf_s("%f", &birthMonth);
if (age <= 25)
{
printf("Congratulations, the age is 25 or less! ");
}
else if (age > 25)
{
printf("Sorry, your guess was too high! ");
}
if (weight <= 128)
{
printf("Congratulations, the weight is 128 or less! ");
}
else if (weight > 128)
{
printf("Sorry, your guess was too high! ");
}
if (birthMonth == "April")
{
printf("Congratulations, the birth month is April! ");
}
else
{
printf("Sorry, your guess was not correct! ");
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
float age;
float weight;
char birthMonth[10];
printf("Enter your guess for age: ");
scanf("%f", &age);
printf("Enter your guess for weight: ");
scanf("%f", &weight);
printf("Enter your guess for birth month: ");
scanf("%s",birthMonth);
if (age <= 25)
{
printf("Congratulations, the age is 25 or less! ");
}
else if (age > 25)
{
printf("Sorry, your guess was too high! ");
}
if (weight <= 128)
{
printf("Congratulations, the weight is 128 or less! ");
}
else if (weight > 128)
{
printf("Sorry, your guess was too high! ");
}
if (strcmp(birthMonth,"April")==0)
{
printf("Congratulations, the birth month is April! ");
}
else
{
printf("Sorry, your guess was not correct! ");
}
return 0;
}
// there you are using "%f" instead of "%s" u can also use gets(); function also
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.