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

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    guys i really need help! else if ..

    guys can you help me identify what is my error?
    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;
           
           
           }
    They keep telling "In function "main"
    and syntax error before "else"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    else has to go with an if. It can't go with "printf("Bill\t: RM%.2f\n", price);", because that's not an if statement. You should invest in some more curly-braces.

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Only the first statement after an if clause will count as part of the if clause. You need to wrap brackets around the entire block if it's more than 1 line. Else goes outside the closing brackets.

    I feel like I have seen this code either on this forum or another.. hmm~

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    hmmm but they only will detect the 1st if statement~ im a beginner in programming, hope you guys can help me =(

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read \007's post #3, though I would call those "brackets" "braces".
    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

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    but once i compile it, the room price never change at all, it is still the delux's price...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your current code.
    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. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    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;
           
           
           }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you should indent your code properly, e.g.,
    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;
    }
    A few other things to note:
    • gets is unsafe: avoid it.
    • Give variables descriptive names. That said, those variables named D, d, etc, are probably unnecessary (and you never initialised them anyway).
    • (rcode == D || d) is probably a mistake. You may have intended to write (rcode == 'D' || rcode == 'd').
    • You repeat code that could have been moved to after the if-else chain.
    • getche is non-standard and you probably don't need it anyway.
    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

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    if(rcode == D || d)

    You are comparing the variable 'rcode' (a single character) to two other variables D or d (also single characters), which you did not bother to initialise.

    what you wanted was to compare 'rcode' to the ASCII value of D or d which requires the letter be surrounded by single quote marks.

    if(rcode == 'D' || 'd')

    or

    if(toupper(rcode) == 'D') //convert the input to upper case so we only have to comper to a capital D
    Last edited by novacain; 07-06-2011 at 10:21 PM. Reason: too late.....
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Code:
    char name[30],rcode,D,d,s,S,T,t;
    You never initialize these variables ( in red), however you try to use them here:
    Code:
     if(rcode == D || d) <---This is not doing what you think it is.
    You shouldn't be using gets. Replace it with fgets.

    EDIT: 3rd one to hit the post button.
    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.

  12. #12
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    General style tips:

    * limit your source code to 80 characters per line

    * don't linear declare a bunch of variables. It's better to declare a variable as close to where it's used as possible and initialize it as soon as you can.

    * don't put code on the same line as a brace, unless it's a comment " */ end of if block */ " etc.

    * put a space after a closing brace

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I know everyone has their own style but I am going to have to say that I don't agree at all with this:
    It's better to declare a variable as close to where it's used as possible and initialize it as soon as you can.
    I tend to think you should declare variables that will be used within a certain scope in the same spot, that way you don't have random declarations running throughout your code. e.g.
    Code:
    int foo(){
    
       my variables....
    
       execution code...
    }
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    I tend to think you should declare variables that will be used within a certain scope in the same spot, that way you don't have random declarations running throughout your code.
    You have to do that anyway when coding with respect to say, C89.

    However, declaring variables near first use does not mean "random declarations running throughout your code".
    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

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Learn something new everyday. So, and forgive my ignorance, what exactly does it mean?
    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.

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