Thread: guys i really need help! else if ..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,415
    Quote Originally Posted by AndrewHunter
    So, and forgive my ignorance, what exactly does it mean?
    That the declaration of a variable should be delayed to the point where it can be sensibly initialised, as opposed to leaving it uninitialised for several lines, or initialised to a default value that will be overwritten anyway and which could be accidentally used incorrectly before that point. This is not "random", unless you are saying that the structure of the flow of control in your code is "random".
    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

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    Thnx all! especially to Lasterlight , it works

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    This is not "random", unless you are saying that the structure of the flow of control in your code is "random".
    In his defense, I think he was using the term "random" facetiously.

    You have to do that anyway when coding with respect to say, C89.
    Not that anyone cares, but the old style is my own personal preference.

  4. #19
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    That the declaration of a variable should be delayed to the point where it can be sensibly initialised, as opposed to leaving it uninitialised for several lines, or initialised to a default value that will be overwritten anyway and which could be accidentally used incorrectly before that point. This is not "random", unless you are saying that the structure of the flow of control in your code is "random".
    Hmm...something to chew on. Thanks Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #20
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    The old style is old for a reason. A better style was developed~ there is documented proof that improvements in style formats like declaring variables close to the initial use drives down possible bugs and creates more readable code. The end result is less work which translates to less money being consumed by the project.

    The old style is of course important to understand and work with from time to time.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by \007 View Post
    The old style is old for a reason.
    It is clear and makes sense.
    Quote Originally Posted by \007 View Post
    A better style was developed~ there is documented proof that improvements in style formats like declaring variables close to the initial use drives down possible bugs and creates more readable code.
    Being able to scatter variable declarations randomly throughout your code does not make it a better style:
    Code:
    printf( "enter a number: " );
    int number;
    scanf( "%d", &number );
    That is not better code IMO.
    Quote Originally Posted by \007 View Post
    The old style is of course important to understand and work with from time to time.
    /facepalm

    Yes, because declaring variables at the start of a block is so confusing, right?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,415
    Quote Originally Posted by quzah
    Being able to scatter variable declarations randomly throughout your code does not make it a better style:
    Agreed: ability != style

    Quote Originally Posted by quzah
    That is not better code IMO.
    Better than what? I'd say that that is not better than:
    Code:
    int number;
    printf( "enter a number: " );
    scanf( "%d", &number );
    Since the printf is related to the scanf and it is unlikely that code that might access number would be inserted in between, so the declaration just before the printf is still sufficiently near first use. However, it is better than:
    Code:
    int number;
    /* ... several lines of code that does not involve number ... */
    printf( "enter a number: " );
    scanf( "%d", &number );
    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

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Justin Chan View Post
    Code:
    #include<stdio.h>
    
    int main()
    {
           
           char name[30],rcode,D,d,s,S,T,t;
           float rprice,price;
           int days;
           
           printf("------------------------------------------\n\tWELCOME TO LEGEND HOTEL\t\t\n------------------------------------------");
           printf("\nRooms : Delux(D)  Twin Sharing(T)  Single(S)\n\n");
           printf("Enter your name\t\t :");
           gets(name);
           printf("Enter Room code\t\t :");
           scanf("%c", &rcode);
           printf("Enter number of days\t :");
           scanf("%d", &days);
           printf("\n\n------------------------------------------\n\tPAYMENT RECEIPT\t\t\n------------------------------------------\n");
           
           
           
                 if(rcode == D || d)
                 {rprice = 200.00;
                 price = rprice * days;
                 printf("Customer Name\t: %s\n", name);
                 printf("Room Type\t: Delux\n");
                 printf("Room Price\t: %.2f\n", rprice);
                 printf("Number of days\t: %d\n", days);
                 printf("Bill\t: RM%.2f\n", price);
                 }
                 else
                 if(rcode == T || t)
                 {rprice = 170.00;
                 price = rprice * days;
                 printf("Customer Name\t: %s\n", name);
                 printf("Room Type\t: Twin Sharing\n");
                 printf("Room Price\t: %.2f\n", rprice);
                 printf("Number of days\t: %d\n", days);
                 printf("Bill\t: RM%.2f\n", price);
                 }
                 
                 else
                 if(rcode == S || s)
                 {rprice = 120.00;
                 price = rprice * days;
                 printf("Customer Name\t: %s\n", name);
                 printf("Room Type\t: Single\n");
                 printf("Room Price\t: %.2f\n", rprice);
                 printf("Number of days\t: %d\n", days);
                 printf("Bill\t: RM%.2f\n", price);
                 }
                 else
                 {price = 0;
                 price = rprice * days;
                 printf("Customer Name\t: %s\n", name);
                 printf("Room Type\t: Invalid\n");
                 printf("Room Price\t: %.2f\n", rprice);
                 printf("Number of days\t: %d\n", days);
                 printf("Bill\t: RM%.2f\n", price);
                 }
           
           
           
           
           
           
           
           
           
           system("pause");
           getche();
           return 0;
           
           
           }
    First, turn the warning levels on your compiler to their maximum ... then treat every warning as an error to be fixed. You'll be amazed how smart your compiler is about helping you write better code. It should be complaining about uninitialized variables, unused variables and invalid conditional statements...

    For example...
    Code:
                 if(rcode == D || d)
    ... simply isn't going to do what you want it to.

    You are obviously wanting to compare the rcode which is typed by the user to find out which letter they've entered. You do that like this...
    Code:
    if (rcode == 'D' || rcode == 'd')
    ... comparing the value of rcode to character constants, not other variables.

    This of course means that your D,d,s,S,T,t variables are unused in the program and can be removed.

    Also you have a large amount of redundent code in your program. For example, you only need to display the customer name and number of days once before entering your if/else if sequence, not on every branch of it.

  9. #24
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Quote Originally Posted by laserlight View Post
    Agreed: ability != style


    Better than what? I'd say that that is not better than:
    Code:
    int number;
    printf( "enter a number: " );
    scanf( "%d", &number );
    Since the printf is related to the scanf and it is unlikely that code that might access number would be inserted in between, so the declaration just before the printf is still sufficiently near first use. However, it is better than:
    Code:
    int number;
    /* ... several lines of code that does not involve number ... */
    printf( "enter a number: " );
    scanf( "%d", &number );
    I feel like you understand what I have been saying. I don't know why Quzah isn't getting it.

    For now on, I will just suggest people read a good styles book-- such as Code Complete.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by \007 View Post
    I don't know why Quzah isn't getting it.
    The ability to scatter variable declarations wherever you feel like in your code as a bad idea. It makes people lazy in that they don't have to think ahead of time about anything they do, and makes your code ugly and harder to read.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Once again, you don't put declarations randomly or scattered at all. You choose the most logical location that is close to the use of it.

    The objective is to not have code like this:

    Code:
    int main(void)
    {
      int i, counter;
    
      /* 100 lines later */
    
      counter = 10;
      for(i = 0; i < counter; i++)
        printf("%d\n", i);
    
      return 0;
    }
    Instead we do like this:


    Code:
    int main(void)
    {
      /* 100 lines later */
    
      int i, counter = 10;
      for(i = 0; i < counter; i++)
        printf("%d\n", i);
    
      return 0;
    }

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by \007 View Post
    The objective is to not have code like this:
    I agree, but picking your own places to declare is only legal under C99. Strict C89 code requires that all variables be declared before any instruction (ie, together at the top of a function and no where else). So it does not really make sense to chide people for adhering to that.

    However, it is good to let them know that they don't have to, since I think most beginner material is strictly C89 but much or most real programming is C99.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by \007 View Post
    Once again, you don't put declarations randomly or scattered at all.
    I don't. But you could, and that is the whole reason for my dislike of the ability to do so.
    Quote Originally Posted by \007 View Post
    The objective is to not have code like this:
    Code:
    int i, counter;
    
      /* 100 lines later */
    
      counter = 10;
      for(i = 0; i < counter; i++)
    It is easier for me to locate counter where I expect it (at the top of the current block, or the top of the function) than to have to hunt for it on every single one of those 100 lines.

    See, you aren't even doing it right now:
    Quote Originally Posted by \007 View Post
    Instead we do like this:
    Code:
    int main(void)
    {
      /* 100 lines later */
    
      int i, counter = 10;
      for(i = 0; i < counter; i++)
        printf("%d\n", i);
    
      return 0;
    }
    You should be doing:
    Code:
    for( int i = 0, counter = 10; i < counter; it++ )

    Quzah.
    Last edited by quzah; 07-07-2011 at 03:00 PM. Reason: making a point
    Hope is the first step on the road to disappointment.

  14. #29
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    My own opinion is that it's just more organized to keep all the variables located at the top of the block. If this causes confusion or "error because I may have assigned it to something else between here and up there," then that just means I would have to correct and learn from that error.

    This is largely about personal preference. I prefer the old ways myself.

    The objective is to not have code like this...
    No, the objective is to have clean, organized code with a consistent style that works the way it should.

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    One solution that resolves both quzah's consern that variables are hard to find after a block and others' concern that variables are not declared where they are used is to liberally use blocks to mark the exact range where a variable can be accessed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thanks Guys...
    By frodonet in forum C Programming
    Replies: 0
    Last Post: 11-03-2007, 08:16 PM
  2. do you guys have...
    By enjoyincubus in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 10:14 PM
  3. Hello guys =)
    By carol.prime in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-22-2006, 10:25 AM
  4. What do you guys think about this?
    By RobR in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 06-07-2006, 11:17 AM
  5. Hey guys, I'm new!
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 10-31-2003, 05:47 PM