Thread: having problems with if and else statements

  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
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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
    {
        // ...
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed