having problems with if and else statements

This is a discussion on having problems with if and else statements within the C Programming forums, part of the General Programming Boards category; hi guys im having a problem with misplaced else statements heres my code Code: #include<stdio.h> #include<conio.h> int dayspassed(int month, int ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    having problems with if and else statements

    hi guys im having a problem with misplaced else statements
    heres my code

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int dayspassed(int month, int day, int year,
    		int smonth, int sday, int syear);
    
    struct library
    { char bno[80];
      char btit[80];
      char author[80];
    };
    
    int main()
    { char buffer[20]={0};
      int day=0, month=0, year=0;
      int sday=0, smonth=0, syear=0;
      struct library book1;
    
    
    
      clrscr();
    
      puts("Please enter the book number: ");gets(book1.bno);
      puts("Please enter the book title: ");gets(book1.btit);
      puts("Please enter the book author/s: ");gets(book1.author);
      puts("Please enter the date borrowed in MM/DD/YY format: ");
      fgets(buffer, sizeof(buffer), stdin);
      sscanf(buffer, "%d/%d/%d", &month, &day, &year);
    
      if((month>12||day>30))
    	{puts("Error!: Invalid date.\n");
    	 printf("\nThe program will exit!");
    	 exit();}
      else
    	printf("\nYou chose %d/%d/%d\n", month, day ,year);
    
    
    
      puts("Input date returned in MM/DD/YY format: ");
      fgets(buffer, sizeof(buffer), stdin);
      sscanf(buffer, "%d/%d/%d", &smonth, &sday, &syear);
      if((smonth>12||sday>30))
    	{puts("Error!: Invalid date.\n");
    	 printf("\nThe program will exit!");
    	 exit();}
    
      clrscr();
    
    
      printf("\n\n\n\t\t\tThe days between %d/%d/%d and %d/%d/%d is %d",
    	 month, day, year, smonth, sday, syear,
      dayspassed(month, day, year, smonth, sday, syear));
    
      printf("\n\t\t\tBook Number is: ");puts(book1.bno);
      printf("\n\t\t\tBook Title is: ");puts(book1.btit);
      printf("\n\t\t\tBook Author: ");puts(book1.author);
      printf("\n\t\t\tDate Borrowed: %d/%d/%d",month, day, year);
      printf("\n\t\t\tDate Returned: %d/%d/%d",smonth, sday, syear);
    
      if((dayspassed(month, day, year, smonth, sday, syear)<4))
    	{printf("\n\nYou have no fine, please return the book to its proper shelf");
    	}
      else((dayspassed(month, day, year, smonth, sday, syear)>4||
    	dayspassed(month, day, year, smonth, sday, syear)<30));
    	{printf("\n\nYour fine is %d, please return the book to its proper shelf",
    	((dayspassed(month, day, year, smonth, sday, syear))*5));
    	getch();}
      if(dayspassed(month, day, year, smonth, sday, syear)>30);
    	{puts("\n\nPress any key to continue.");
    	printf("\n\nP.S. You also have a notice!, bye!");}
    
      getch();
    }
    
     int dayspassed(int month, int day, int year,
    		int smonth, int sday, int syear)
    
     { int diffday=0, diffmonth=0, diffyear=0;
       int days=0;
    
       diffday=sday-day;
       diffmonth=smonth-month;
       diffyear=syear-year;
    
     days=diffday+(diffmonth*30)+(diffyear*360);
    
     return(days);
     }
    
    the one that was bold was the one i am having problem with kindy help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    it works but when the first if is performed the else performs too and the other if.. either it is greater than 30 or whatever it still does perform..

    Code:
    if((dayspassed(month, day, year, smonth, sday, syear)<4))
    	{printf("\n\nYou have no fine, please return the book to its proper shelf");
    	}
      else((dayspassed(month, day, year, smonth, sday, syear)>4||
    	dayspassed(month, day, year, smonth, sday, syear)<30));
    	{printf("\n\nYour fine is &#37;d, please return the book to its proper shelf",
    	((dayspassed(month, day, year, smonth, sday, syear))*5));
    	getch();}
      if(dayspassed(month, day, year, smonth, sday, syear)>30);
    	{puts("\n\nPress any key to continue.");
    	printf("\n\nP.S. You also have a notice!, bye!");}

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    it works but when the first if is performed the else performs too and the other if.. either it is greater than 30 or whatever it still does perform..
    Ah. Here's the equivalent code, but with indentation, inclusion of otherwise optional braces, and exclusion of superfluous parentheses:
    Code:
    if (dayspassed(month, day, year, smonth, sday, syear) < 4)
    {
        printf("\n\nYou have no fine, please return the book to its proper shelf");
    }
    else (dayspassed(month, day, year, smonth, sday, syear) > 4
        || dayspassed(month, day, year, smonth, sday, syear) < 30)
    {
        /* do nothing */;
    }
    
    {
        printf("\n\nYour fine is &#37;d, please return the book to its proper shelf",
               dayspassed(month, day, year, smonth, sday, syear) * 5);
        getch();
    }
    
    if (dayspassed(month, day, year, smonth, sday, syear) > 30)
    {
        /* do nothing */;
    }
    
    {
        puts("\n\nPress any key to continue.");
        printf("\n\nP.S. You also have a notice!, bye!");
    }
    Do you see the problem now?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    dude nothings changed

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    dude nothings changed
    Eh, that's exactly what I said. Nothing has changed, except that it should be more obvious what is (are) the mistake(s).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    my problem is i want the other statements to be disregarded when the first IF was performed..

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    my problem is i want the other statements to be disregarded when the first IF was performed..
    Yes, so what you want is to write it like:
    Code:
    if (condition1)
    {
        // ...
    }
    else if (condition2)
    {
        // ...
    }
    else
    {
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    hm...my compiler says else if is unknown.. my compiler is borland v2.01 what compiler should i use?

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by acohrockz21 View Post
    hm...my compiler says else if is unknown.. my compiler is borland v2.01 what compiler should i use?
    Do you put it inside function? if is known to any C compiler
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    hm...my compiler says else if is unknown..
    What is the code and the exact error message?

    my compiler is borland v2.01 what compiler should i use?
    Something newer, like gcc 3 or 4, or MSVC8 or 9 (from Microsoft Visual Studio 2005 and 2008 respectively). However, for the purposes of my example an outdated compiler should still work - this is basic C syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21