error in small code

This is a discussion on error in small code within the C Programming forums, part of the General Programming Boards category; hi all i Found this code i Tried to compiled it but he show error It needs to be some ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    error in small code

    hi all

    i Found this code
    i Tried to compiled it
    but he show error
    It needs to be some minor adjustments

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    float average(float ary, float count)
    
    {
    
    	float tot = 0.0;
    
            int i;
    
    
    
            for(i = 0; i < count i++)
    
                    tot += ary;
    
    
    
            return(tot / count);
    
    }
    
    int main()
    
    
    
    {
    
    
    
    	printf("\n\t\t\t****************************\n");
    
    	printf("\t\t\t****************************\n");
    
    	printf("\t\t\t******** L-G-H Team ********\n");
    
    	printf("\t\t\t****************************\n");
    
    	printf("\t\t\t****************************"\n);
    
    	printf("\t\t            the result is %f\n", averag(5.5, 3.5));
    
    	printf("\t\t\t        you got it :D\n\n");
    
    	printf(" press any key to exit...\n");
    
    
    
    getche();
    
    return 0;
    
    }



    please

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Aaaaaand, what was the error? Probably something to do with that non-standard getche() function?

    Or maybe the fact that you're calling averag() instead of the average()?

    Or maybe the for() loop that's missing a semi-colon?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    the error

    error.c: In function `average':
    error.c:8: error: syntax error before "i"
    error.c: In function `main':
    error.c:21: error: stray '\' in program
    error.c:21: error: syntax error before "n"


    itsme86

    i know but I do not understand the language of c

    help ?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> for(i = 0; i < count i++)

    for(i = 0; i < count; i++)

    is one error there anyway.

    EDIT - >> Or maybe the for() loop that's missing a semi-colon?
    I should have read your reply first :/

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("\t\t\t****************************"\n);
    Compare this line with the previous line, and you will notice a difference. Fix this and the other errors described by itsme.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by twomers
    >> for(i = 0; i < count i++)

    for(i = 0; i < count; i++)

    is one error there anyway.

    EDIT - >> Or maybe the for() loop that's missing a semi-colon?
    I should have read your reply first :/
    i do it but i see this error

    error.c: In function `main':
    error.c:21: error: stray '\' in program
    error.c:21: error: syntax error before "n"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by swoopy
    > printf("\t\t\t****************************"\n);
    Compare this line with the previous line, and you will notice a difference. Fix this and the other errors described by itsme.
    i do it but i see same error

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    >>error.c:21: error: stray '\' in program

    lets examine this...
    error.c that is the file it is trying to compile.
    21 is the line the message is about.
    error means that there is something wrong
    stray means that there is something where it shouldn't be.
    '\' is the aformentioned something where it shouldn't be.
    in program is pretty obvious.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Wraithan

    so what the Output of code ?


    please I do not understand the language of c

    i'm just play in wargame

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is that you Dean?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote Originally Posted by swoopy
    Is that you Dean?
    what you mean ??

  12. #12
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Change

    Code:
    printf("\t\t\t****************************"\n);
    To

    Code:
    printf("\t\t\t****************************\n");

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Rather than using the non-standard function getche() to pause the screen, try these FAQs:

    [But don't use the system("pause") option; the getchar() one is the best.]

    Code:
    float average(float ary, float count)
    
    {
    
    	float tot = 0.0;
    
            int i;
    
    
    
            for(i = 0; i < count i++)
    
                    tot += ary;
    
    
    
            return(tot / count);
    
    }
    I seriously doubt that code does what you intended it to, even after you fix the missing semicolon error.
    • Your count variable (the number of items to average?) is a float. You can't have 1.5 numbers, and i, which you compare it to, is an int. I think you meant to make it an int.
    • ary, from the name and context, should probably be an array. As written, your code totals ary count times, then divides the result by count, returning that value. So if ary is 5 and count is 3, it does something like this: (5+5+5)/3. If you don't know how to use arrays, read the tutorial: http://www.cprogramming.com/tutorial/c/lesson8.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    thanks

    but can you fix the code ?

    i try but i can't

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > i Found this code
    Well there's your problem - you learn a hell of a lot more from actually writing your own stuff rather than just grabbing any old stuff off the net.

    > i try but i can't
    Where have you tried?
    Read the posts,
    Make some changes,
    See what new / different error messages appear,
    Think about what the messages mean.

    When you next get stuck, post your latest code.

    I expect to see a good few hours to have elapsed rather than some quick reply after about 5 minutes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with this small code
    By axe in forum C Programming
    Replies: 10
    Last Post: 01-06-2006, 07:04 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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