Syntax Error

This is a discussion on Syntax Error within the C Programming forums, part of the General Programming Boards category; I think a silly syntax error then; i post the code.. Code: #include <stdio.h> #include <stdlib.h> main() { int n; ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    8

    Syntax Error

    I think a silly syntax error then; i post the code..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    
    { int n;              
      int step=0;
    
        scanf("%d",&n);
        if(n==1)return 0; 
        if (n%2==0){
              n=n/2;
    	  step++;
        else 
              n=3*n+1;  
              step++;}
         
       printf("%d",step);
        return 0;
    }

    i get back from gcc:

    ' error: syntax error before "else" '

    then, the error stands in 'step++', i guess.. but don't know how to tweak it.. i need the function add +1 to 'step' for both cases, even and odd inputs.

    any clue? thanks to all

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Code:
        if (n%2==0){
              n=n/2;
    	  step++;
        } else {   // Note the extra braces 
              n=3*n+1;  
              step++;}
    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.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    I prefer:
    Code:
        if ((n % 2) == 0)
        {
            n = n / 2;
            step++;
        } 
        else 
        {
            n = 3 * n + 1;  
            step++;
        }
    gg

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    thanks for the input

    something strange is happening.

    compile: ok

    the function assigns always 1 thou, whatever is the input.

    here's an example;

    for the input 1 -> function returns 0 and it is right;
    but, if i give 3,4,7,8,10 or whatever else, it returns me always 1.


    checks seems ok, compile ok, but function don't calculate in the right way the function..

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Well... it would help if we knew what your code is trying to do with the step value. I would guess you're missing a loop somewhere.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Given the lack of any loop at all in your code, what do you expect?

    Sure, each branch of your if/else has step++, so all that you really have is (reduced)
    Code:
        scanf("%d",&n);
        if(n==1)return 0; 
        step++;
        printf("%d",step);
    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.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    sure

    it should work as in this case;

    input 10.

    10 is even then 10/2 = 5 (step=1)
    5 is odd then 5*3+1 = 16 (step=2)
    16 is even then 16/2=8 (step=3)
    8 is even then 8/2=4 (step=4)
    4 is even then 4/2=2 (step=5)
    2 is even then 2/2=1 (step=6)

    when receives 1, function stops.

    then, if i give the input 10 function should return me 6.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well two people told you to use a loop, the rest is up to you.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    does it mean i have to change the structure of this code?

    i'm not asking someone will do a job for me, absolutely.
    i've checked the code, seems to be ok, check for the '1' is ok and the return is correct, but checks for the other input fail; this is really strange, and i don't understand at the moment, where the structure fails and doesn't give me right return back.

    thanks for your help, thou.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    How about
    while ( n != 1 )

    Or something
    *shrug*
    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.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    ok, man i was only asking what's going on; don't know why your 'shrug'.

    i don't understand, however, why the 'if' construct doesn't work properly. he makes his job nicely only for the input=1. on the other fails.
    otherwise, all checks with the 'while' are correct.

    thanks.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Code:
        if(n==1)return 0;  // Nope, this isn't a while
        if (n%2==0){ // Nope, this isn't a while
              n=n/2;
    	  step++;
        else // Nope, this isn't a while
              n=3*n+1;  
              step++;}
    Where exactly is your while loop?
    How about posting your latest code, if you're still having problems.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    8
    the function works fine, while looping.

    i don't really understand why the 'if' goes all the checks, over the '1', to the hell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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