Thread: What is it I'm forgetting!?

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    21

    What is it I'm forgetting!?

    I've been sitting here wondering what it is I'm forgetting and why it is NOT compiling for more than an hour, I've done it via pico and codepad

    Can someone please help!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    
    //HW2
    
    {
    int n=0;
    int m=0;
    int a=0;
    
            printf("Enter the amount of Adults:\n");
    
            scanf("%d", &n);
    
    
        if (n>2 || n<2) {
            printf("Error! you are required to input 2 adults\n");
        exit(1);
        }
    
            printf("Enter the amount of Children:\n");
    
            scanf("%d", &m);
    
    
        if (m>1 || m<1) {
            printf("Error! you are required to input 1 child\n");
        exit(1);
        }
    
            printf("How old is your child?:\n");
    
            scanf("%d", &a);
    
        if (a=<6)
        printf("Your child may enter the park for free.\n");
            else if (a => 7 || a =< 12)
        printf("Your child will get 10% off\n");
            else if (a > 12)
        printf("You do not have a child and they must pay the adult free.\n");
    The errors I am getting via codepad: http://codepad.org/hs8IbtAI

    The errors via pico

    Code:
    hw2.c: In function 'main':
    hw2.c:37: error: expected expression before '<' token
    hw2.c:39: error: expected expression before '>' token
    hw2.c:42: error: expected declaration or statement at end of input
    Last edited by iamtazb; 05-25-2012 at 11:15 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Guess "=<" is not right; try "<=".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    It's because it's less-than-or-equal to i.e.
    Code:
    if (a <= 6)
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    21
    Awesome got rid of 2/4 errors!

    Now I'm getting

    (??what is this) hw2.c: In function 'main':
    (last line of the program) hw2.c:42: error: expected declaration or statement at end of input

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Post your updated code -- all of it. Do you have a closing bracket for main? You should declare main to explicitly return an int, and put a return 0; at the end of main as well.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    21
    Quote Originally Posted by anduril462 View Post
    Post your updated code -- all of it. Do you have a closing bracket for main? You should declare main to explicitly return an int, and put a return 0; at the end of main as well.
    here is my updated script with still the 2 errors:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
    //HW2
    
    {
    int n=0;
    int m=0;
    int a=0;
    
            printf("Enter the amount of Adults:\n");
    
            scanf("%d", &n);
    
    
            if (n>2 || n<2) {
            printf("Error! you are required to input 2 adults\n");
            exit(1);
            }
    
            printf("Enter the amount of Children:\n");
    
            scanf("%d", &m);
    
    
            if (m>1 || m<1) {
            printf("Error! you are required to input 1 child\n");
            exit(1);
            }
    
            printf("How old is your child?:\n");
    
            scanf("%d", &a);
    
            if (a <=6 )
            printf("Your child may enter the park for free.\n");
                    else if (a >= 7 || a <= 12)
        printf("Your child will get 10% off\n");
                    else if (a > 12)
            printf("You do not have a child and they must pay the adult fee.\n");
    
    }
    here is the errors I am getting

    Code:
    hw2.c: In function 'main':
    hw2.c:44: error: expected declaration or statement at end of input

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try this for main: int main(int argc, char **argv)

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Learn to indent your code properly, and you would see that your brackets don't match up. Read this: SourceForge.net: Indentation - cpwiki

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
        //HW2
    
        {
            int n = 0;
            int m = 0;
            int a = 0;
    
            printf("Enter the amount of Adults:\n");
    
            scanf("%d", &n);
    
            if (n > 2 || n < 2) {
                printf("Error! you are required to input 2 adults\n");
                exit(1);
            }
    
            printf("Enter the amount of Children:\n");
    
            scanf("%d", &m);
    
            if (m > 1 || m < 1) {
                printf("Error! you are required to input 1 child\n");
                exit(1);
            }
    
            printf("How old is your child?:\n");
    
            scanf("%d", &a);
    
            if (a <= 6)
                printf("Your child may enter the park for free.\n");
            else if (a >= 7 || a <= 12)
                printf("Your child will get 10% off\n");
            else if (a > 12)
                printf("You do not have a child and they must pay the adult fee.\n");
    
        }
    Now, it is clear that you have an extra opening brace that should be removed.

    Also, you should declare main as explicitly having a return type of int.
    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
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    if (n > 2 || n < 2)
    Seriously? Try:
    Code:
    if (n != 2)
    Also, no need to have 3 ints when they're only being used once linearly.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    main()
    {
        //HW2
    
        int n = 0;
        int m = 0;
        int a = 0;
    
        printf("Enter the amount of Adults:\n");
        scanf("%d", &n);
    
        if (n != 2) {
            printf("Error! you are required to input 2 adults\n");
            exit(1);
        }
    
        printf("Enter the amount of Children:\n");
        scanf("%d", &m);
    
        if (m != 1) {
            printf("Error! you are required to input 1 child\n");
            exit(1);
        }
    
        printf("How old is your child?:\n");
        scanf("%d", &a);
    
        if (a <= 6)
            printf("Your child may enter the park for free.\n");
        else if (a >= 7 || a <= 12)
            printf("Your child will get 10% off\n");
        else if (a > 12)
            printf("You do not have a child and they must pay the adult fee.\n");
        
        return 0;
        
    }
    Last edited by knocknock; 05-25-2012 at 01:39 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your variable names are awful, a could be for adults or age. n and m make no sense. Don't try to save a few keystrokes at the cost of clarity. Call them num_adults, num_children and child_age. Actually, why do you even bother asking for input for num_adults and num_children if there is only one allowed value for each? Typically, if you have a variable that can only ever have exactly one value, and it never changes, you make a constant. Besides, you aren't using their values for anything, so use them for something or get rid of them all together.

    Also, when do you expect this to be false?
    Code:
    else if (a >= 7 || a <= 12)

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    21
    Thanks for all the help guys! I cleaned up the code and will post an updated version in just a bit

  14. #14
    Registered User
    Join Date
    May 2012
    Posts
    21
    Quote Originally Posted by nonoob View Post
    Try this for main: int main(int argc, char **argv)
    My prof never taught us to

    a) declare main
    b) add the stuff in the bracket

    Is it beneficial to do it that way or just keep it at

    Code:
    main(){
    }
    for now?

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by iamtazb View Post
    My prof never taught us to

    a) declare main
    b) add the stuff in the bracket

    Is it beneficial to do it that way or just keep it at

    Code:
    main(){
    }
    for now?
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2005, 06:11 PM