Like Tree7Likes
  • 1 Post By nonoob
  • 1 Post By anduril462
  • 2 Post By laserlight
  • 1 Post By memcpy
  • 1 Post By knocknock
  • 1 Post By anduril462

What is it I'm forgetting!?

This is a discussion on What is it I'm forgetting!? within the C Programming forums, part of the General Programming Boards category; I've been sitting here wondering what it is I'm forgetting and why it is NOT compiling for more than an ...

  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
    2,541
    Guess "=<" is not right; try "<=".

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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,451
    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,831
    Try this for main: int main(int argc, char **argv)
    iamtazb likes this.

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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    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.
    iMalc and iamtazb like this.
    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

  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.
    iamtazb likes this.

  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.
    iamtazb likes this.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    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)
    iamtazb likes this.

  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
    2,541
    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
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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