Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: why won't my if-else statement work?  (Read 3298 times)

0 Members and 1 Guest are viewing this topic.

trigger349

  • Guest
why won't my if-else statement work?
« on: October 10, 2009, 09:27:17 AM »
#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);
}
}

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. :-\

itburnswhenipee



    Beginner

    Thanked: 1
    Re: why won't my if-else statement work?
    « Reply #1 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); {)

    Salmon Trout

    • Guest
    Re: why won't my if-else statement work?
    « Reply #2 on: October 10, 2009, 01:19:52 PM »
    are you supposed to have ; at the end of an if statement? (if (9 = x) { not if (9= x); {)

    No.


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: why won't my if-else statement work?
    « Reply #3 on: October 10, 2009, 01:30:55 PM »
    What language?   :)

    Salmon Trout

    • Guest
    Re: why won't my if-else statement work?
    « Reply #4 on: October 10, 2009, 01:53:11 PM »
    What language?   :)

    Sort of looks like C I think

    Code: [Select]
    #include <stdio.h>

    itburnswhenipee



      Beginner

      Thanked: 1
      Re: why won't my if-else statement work?
      « Reply #5 on: October 10, 2009, 04:17:20 PM »
      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?