help please i have errors and dont know how to fix them new at this

This is a discussion on help please i have errors and dont know how to fix them new at this within the C Programming forums, part of the General Programming Boards category; so im new to the c programming thing but im starting to find it quite intesting so i am trying ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    help please i have errors and dont know how to fix them new at this

    so im new to the c programming thing but im starting to find it quite intesting so i am trying to make a program that converts mph to fps but i keep getting errors can anyone help me heres the program

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    { 
    
    int mph =0
    double fps, increment
    printf("please input increment between 0-80 mph \n");
    scanf("%lf", &increment);
    mph=mph + increment
    
    while mph <=80
    
    fps=mph*5280/3600
    printf("%5.0f %5.1f" \n, mph, fps);
    
    }
    i get errors on lines 12 saying that i have a syntax error before double
    line 14 says increment is undeclared (first use this function)then on 14 again sainf that each undeclared identifier is reported only once for each function it appearsin.) and lie 17 says syntax error before "mph"

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    You need to terminate your line(s) with semi-colons ( ; )

    And you need to look up how while loops work.
    Code:
    while (condition is true)
    {
        /* run this code */
    }
    In your case, a for loop is likely a better option:
    Code:
    for(mph = 0; mph <= 80; mph += increment)
    {
        /* run this code */
    }
    Last edited by rags_to_riches; 02-19-2009 at 02:03 PM.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    also while loop should be like
    Code:
    while ( condition )
    {
       statement_1;
       ...
       statement_n;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    ok thank guys

    but what do i do about that syntax error on line 12

    and line 17 increment undeclared and the syntax before ")" token whatever than my mean my c book isnt helpin me much with debugging

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    and my error in "main"

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well, show us your updated code. So that we could have a look at. And I also notices this in the printf statement:

    Code:
    printf("%5.0f %5.1f" \n, mph, fps);
    The '\n' character is an escape sequence character, which would always be part of a string. So that should be within the double quotes. Like

    Code:
    printf("%5.0f %5.1f \n", mph, fps);
    And you should also look at how to indent you could. May be you will start loving your own code

    Post your updated code. Let us see what have you got so far?

    edit: Every statment in C language should terminate with the ';'. Make a note of that. So you write a code and thats SHOULD end with the ';' unless its some sort of conditional function!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    edit: Every statment in C language should terminate with the ';'. Make a note of that. So you write a code and thats SHOULD end with the ';' unless its some sort of conditional function!
    What that last bit means is that
    Code:
    if (x) ;
        printf("x is true\n");
    is "not right" - however, the compiler will happily compile it, and only few compilers will even say things like "Huh? did you mean to do "if (x) then do nothing" - it is not uncommon that even experienced programmers make that mistake from time to time. It gets extra bad when the condition is false only rarely - say in the above that we expect x to be true most of the time. It may be hard to discover that it has printed an extra "x is true" line for the time x was NOT true.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I did actually specify "unless it is some sort of conditional function. All other statments should end with the ';'.

    Originally Posted by ssharish2005
    edit: Every statment in C language should terminate with the ';'. Make a note of that. So you write a code and thats SHOULD end with the ';' unless its some sort of conditional function!
    Edit: May be i could have made much more clear

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    68

    Post

    You forgot a semi colon, another way to write it would be:
    mph += increment;

    Quote Originally Posted by magic3663 View Post
    so im new to the c programming thing but im starting to find it quite intesting so i am trying to make a program that converts mph to fps but i keep getting errors can anyone help me heres the program

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    { 
    
    int mph =0
    double fps, increment
    printf("please input increment between 0-80 mph \n");
    scanf("%lf", &increment);
    mph=mph + increment           <----  need a semi colon,
    
    while mph <=80
    {
          fps=mph*5280/3600
          printf("%5.0f %5.1f" \n, mph, fps);
    }
    i get errors on lines 12 saying that i have a syntax error before double
    line 14 says increment is undeclared (first use this function)then on 14 again sainf that each undeclared identifier is reported only once for each function it appearsin.) and lie 17 says syntax error before "mph"
    Last edited by madmax2006; 02-19-2009 at 04:34 PM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by madmax2006 View Post
    You forgot a semi colon
    Or a few... Which has already been discussed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >You forgot a semi colon
    there is more than a semi colon, which the OP has to sort out.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    He was asking specifically what was wrong with that line (or close to there)......

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    thanks everyone for your help ill keep you updated ill post what i have so far hopefully it works

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    this is the current update with you all's help

    this is the code up date

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    { 
    
    int mph =0
    double fps, increment
    printf("please input increment between 0-80 mph \n");
    scanf("%lf", &increment);
    mph += increment;           
    
    for ( mph <= 80; mph += increment );
    {
          fps=mph*5280/3600
          printf("%5.0f %5.1f \n", mph, fps);
    }
    Errors:
    in function main
    12 syntax error before "double"
    14 "increment" undeclared (first use in this function)
    14 (Each undeclared identifier is reported only once for each function it appears in.)
    17 syntax error before ")" token
    19 "fps" undeclared (first use in this Function)
    22 syntax error at end of input

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well, how many times should we have to point you out? Where are the semi-colns ????

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    { 
        int mph =0 ;
        double fps, increment ;
        printf("please input increment between 0-80 mph \n");
        scanf("%lf", &increment);
        
        mph = mph + increment;
        
        for ( mph <= 80; mph += increment ) ; <-- why do you need this?? take this out     
        {
           fps=mph*5280/3600 ;
           printf("%5.0f %5.1f\n", mph, fps);
        }
        return 0;
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

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

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