Thread: is debugging syntax error really this hard??

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    is debugging syntax error really this hard??

    Code:
    /*Team 6 
    
    for Nam Nguyen, 
    
     */
    
    #include <stdio.h>
    #include <math.h>
    
    #define R1 100 /* R1 value never changes, so it becomes a constant*/
    
    int main()
    {
        int R2, RL;
        float current, voltage;
        double PR1;  /*power consumed by R1*/
        double PR2;  /*power consumed by R2*/
        double PRL;  /* power consumed by RL*/
    
      /* All input resistance values will be in ohms */
    
     /*Ask user for value if R2: */
     R2_Input: printf("Please enter the value of R2 in ohms: /n" );
     scanf("%d", &R2);
    
     /*Check the input*/
     if (R2<0 II R2>10000)
    {  
       printf("R2 must be between 0 and 10,000 ohms; please enter another value: /n" );
       goto R2_Input;
     }
     /*Ask the user to enter the value of RL: */
     RL_Input :printf("Please enter the value for RL in ohms: /n" );
     scanf("%d", &RL);
    
     /*Verify the load resistance*/
     if (RL <100 II RL > 5000)
     {
         printf("The value of RL must be between 100 and 5000 ohms inclusive; please enter another value: /n" );
         goto RL_Input;
     }
    
    /*Ask the user for the value of the voltage source: */
    Volt_Input :printf("Please enter the value of the voltage source in volts: /n");
    scanf("%.1f", &voltage);
    
     /*Verify the range of the voltage: */
     if(voltage < 1 && voltage > 15)
     {
         printf("The voltage value must be between 1 and 15 to the nearest tenth. Please enter another value: /n");
         goto Volt_Input;
      }
    
      /* Calculate the current: */
     current = voltage/(R1 + R2 + RL);
    
    /*Power is calculated in watts*/
    /*Calculate the power for R1: */
    PR1 = R1*pow (current, 2);
    
    /*Calculate the power for R2: */
    PR2 = R2*pow (current, 2);
    
    /*Calculate the power for RL: */
    PRL = RL*pow(current, 2);
    
    /*Construct output table for the current and all power values: */
    printf("/tCurrent /t /t/t PR1/t PR2/t prl/n");
    printf("/n");
    printf("/t%10.4f/ t/t/t%10.4f/t%10.4f/n", current, PR1, PR2, PRL);
    
    /*Output message: */
    if (PR1+PR2) ==PRL)
    {
       printf("NOTE: (PR1 + PR2) = PRL /n");
     }
     else 
     {
        printf("NOTE: (PR1 + PR2)  != PRL /n");
     }
    
     getchar();
     return(0);
    }

    Building c3.obj.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ')' but found 'II'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2048: Undeclared identifier 'II'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ';' but found 'R2'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ';' but found ')'.

    hey, this is my first post. I'm new to c, but I'm having trouble understanding syntax errors. From what ive read, it means ive made a mistake on a grammar problem. I dbl checked most of the stuff up top and they seem to match the program codes from the book. i don't undertand the problem. can someone offer a set of fresh eyes please. thanks!

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Logical OR is || ('pipe' character), not II (capital i)

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    II
    is not the same as

    Code:
    ||
    The second is the logical or operator.

    Tim S

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (R2<0 II R2>10000)
    I'm guessing you mean ||
    This is vertical bar, not upper-case I

    Oh, and you should replace your goto with a while loop.
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And fwiw... no debugging syntax errors is not difficult...

    Code:
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ')' but found 'II'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2048: Undeclared identifier 'II'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ';' but found 'R2'.
    C:\Documents and Settings\11078378\My Documents\c3.c(27): error #2001: Syntax error: expected ';' but found ')'.
    Filename
    Line Number
    Error report

    Open the file, go to the line number... fix the reported error.
    Last edited by CommonTater; 08-20-2011 at 06:49 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Additionally, as a style note, too much commenting is as bad as no comments at all. In your case all the comments take away the readability of the code and make it harder to find errors. Unless you are specifically instructed to comment every line, don't.
    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.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by nanoguy330
    Code:
    R2_Input: printf("Please enter the value of R2 in ohms: /n" );
     scanf("%d", &R2);
    
     /*Check the input*/
     if (R2<0 II R2>10000)
    {  
       printf("R2 must be between 0 and 10,000 ohms; please enter another value: /n" );
       goto R2_Input;
     }
    Ah ohh goto statment. Never seen them in a while. You know it rather not a good practice using goto statment. You can get away with using loops? Like something bellow

    Code:
    do
    {
    	printf("Please enter the value of R2 in ohms: /n" );
    	scanf("%d", &R2);
    } while( R2<0 || R2>10000 );
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Additionally, as a style note, too much commenting is as bad as no comments at all. In your case all the comments take away the readability of the code and make it harder to find errors. Unless you are specifically instructed to comment every line, don't.
    While I do agree with you... It may look quite different in a syntax highlighting editor...

    is debugging syntax error really this hard??-codesnip-png

    I typically quote the daylights out of my code, because I know that a year from now I will find parts of it hard to follow... But notice how the comments are faded so as not to interfere with readability....

    Now if he's working plain text or hands that over to someone working plain text, yep, that's way too much commenting and it definately does make it harder to follow his code...

    And... eyuck... I see goto! Not good.
    Last edited by CommonTater; 08-20-2011 at 06:42 PM.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Right angle lines all over the code.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    While I do agree with you... It may look quite different in a syntax highlighting editor...

    I typically quote the daylights out of my code, because I know that a year from now I will find parts of it hard to follow... But notice how the comments are faded so as not to interfere with readability....
    I do agree that a syntax highlighting editor alleviates some of this, and it is a style point hence everyone is allowed to come up with their own practice. I just don't necessarily see anything added by comments that echo what the code is doing. If proper descriptive variable names are used, you shouldn't required a "this line calculates power" comment. In fact in the OPs code his comments didn't expand on anything, he didn't even bother to explain what R1 is in his comment. Additionally, I see very little use in "this line prints output" followed by a printf() statement. But again, just my opinion.

    Quote Originally Posted by CommonTater View Post
    And... eyuck... I see goto! Not good.
    Haha....now this is where a syntax editor would come in handy. It should automatically highlight and bold goto in red and then insert a frowny face at the end of the line.
    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.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Right angle lines all over the code.
    Nope... only there when the cursor is on a brace... (In Pelles, anyway) ... the dotted line connects to the matching brace.
    Move the cursor off the brace... line gone.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    I do agree that a syntax highlighting editor alleviates some of this, and it is a style point hence everyone is allowed to come up with their own practice. I just don't necessarily see anything added by comments that echo what the code is doing. If proper descriptive variable names are used, you shouldn't required a "this line calculates power" comment. In fact in the OPs code his comments didn't expand on anything, he didn't even bother to explain what R1 is in his comment. Additionally, I see very little use in "this line prints output" followed by a printf() statement. But again, just my opinion.
    Oh, hey... I'm not disagreeing... I know I sometimes overcomment ... I mean //show pathname and then perfectly obvious code... but like everything else about me it's a "Rogue" habit I've somehow gotten into... In my own defense, I wrote a whole 10 line function with no comments this morning...

    Haha....now this is where a syntax editor would come in handy. It should automatically highlight and bold goto in red and then insert a frowny face at the end of the line.
    I'm just waiting until they make that into the syntax error it should be...

    My all time favorite goto found in some old Turbo C code I downloaded while learning...
    Code:
    // bunch of stuff.
    
    if (input == NULL)
      goto Oh_F***_This_Is_All_Wrong;
    
    // about 5 more lines of code
    
    Oh_F***_This_Is_All_Wrong :
    Exit (1);
    And I'm betting you can guess what to substitute for the stars...
    Last edited by CommonTater; 08-20-2011 at 07:26 PM.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    And I'm betting you can guess what to substitute for the stars...
    "ool" - lol

    In any case, goto is of the level of inline asm, and should be used at most equally sparingly.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ... so as not to interfere with readability...
    The main interference with readability is the bracing style (post #5). At a minimum, closing braces should be first on the line, and in the same column of the statement that started the block. Discerning the scope should be as easy as possible, not a bracket hunt.

    gg

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    "ool" - lol

    In any case, goto is of the level of inline asm, and should be used at most equally sparingly.
    Hint: There's a "c", a "k" and a "u" in there... but not necessarily in that order.

    LOL....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging error
    By vexed in forum C Programming
    Replies: 3
    Last Post: 03-08-2011, 01:44 PM
  2. Debugging error
    By vexed in forum C Programming
    Replies: 5
    Last Post: 03-08-2011, 11:46 AM
  3. error C2061: syntax error : identifier
    By maninboots in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2009, 05:40 AM
  4. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  5. Syntax too hard for computer?
    By David Somekh in forum C Programming
    Replies: 3
    Last Post: 04-10-2007, 05:16 AM

Tags for this Thread