1.

Solve : why won't my if-else statement work??

Answer»

#include
#define p printf
#define s scanf
int year, month, age, currentdate, currentmonth, birthmonth, birthdate,
numofmonths, daysalive;
main ()
{
p("Welcome to my age calculating program!\n");
p("The program will CALCULATE the number of days you have live since birth.\n");
p("Note: The result will not be EXACT because of leap years.\n");
p("Input age: ", age);
s("%d;", &age);
{
for (currentmonth = 1;
currentmonth <= 12;
currentmonth++)

p("%d \n", currentmonth);

}
p("Input current month: ", currentmonth);
s("%d;", &currentmonth);
p("Input current date: ", currentdate);
s("%d", &currentdate);
p("Input birth month: ", birthmonth);
s("%d", &birthmonth);
p("Input birth date: ", birthdate);
s("%d", &birthdate);
if (currentmonth >= birthmonth);
{
daysalive = (age * 365) + (currentmonth - birthmonth) * 30 + (30 - birthdate) + currentdate;
p("The number of days you have lived is about %d", daysalive);
s("%d", daysalive);
}
else
(currentmonth <= birthmonth);
{
daysalive = (age * 365) + (12 - birthmonth) * 30 + (currentmonth * 30) + (30 - birthdate) + currentdate;
p("The number of days you have lived is about %d", daysalive);
s("%d", daysalive);
}
}

I checked everything, still it has errors.
It says: Error: expected primary-expression before "else''
Error: expected ';' before "else"

Could you please help? That's my only problem. I need it to work, as in both conditions.
are you supposed to have ; at the end of an if statement? (if (9 = x) { not if (9= x); {)QUOTE from: itburnswhenipee on October 10, 2009, 11:48:24 AM

are you supposed to have ; at the end of an if statement? (if (9 = x) { not if (9= x); {)

No.

What language? Quote from: Geek-9pm on October 10, 2009, 01:30:55 PM
What language?

Sort of looks like C I think

Code: [Select]#include <stdio.h>
Code: [Select]#include <stdio.h>
#define p printf
#define s scanf
int year, month, age, currentdate, currentmonth, birthmonth, birthdate,
numofmonths, daysalive;
main ()
{
p("Welcome to my age calculating program!\n");
p("The program will calculate the number of days you have live since birth.\n");
p("Note: The result will not be exact because of leap years.\n");
p("Input age: ", age);
s("%d;", &age);
{
for (currentmonth = 1;
currentmonth <= 12;
currentmonth++)

p("%d \n", currentmonth);

}
p("Input current month: ", currentmonth);
s("%d;", &currentmonth);
p("Input current date: ", currentdate);
s("%d", &currentdate);
p("Input birth month: ", birthmonth);
s("%d", &birthmonth);
p("Input birth date: ", birthdate);
s("%d", &birthdate);
if (currentmonth >= birthmonth)
{
daysalive = (age * 365) + (currentmonth - birthmonth) * 30 + (30 - birthdate) + currentdate;
p("The number of days you have lived is about %d", daysalive);
s("%d", daysalive);
}
else (currentmonth <= birthmonth);
{
daysalive = (age * 365) + (12 - birthmonth) * 30 + (currentmonth * 30) + (30 - birthdate) + currentdate;
p("The number of days you have lived is about %d", daysalive);
s("%d", daysalive);
}
}
modified it, got rid of ; on first if and it compiled FINE for me, what compiler are you using?


Discussion

No Comment Found