Help with beginner C code

Discussion in 'iOS Programming' started by Johnny5alive, Aug 20, 2011.

  1. Johnny5alive macrumors newbie

    Joined:
    Aug 1, 2011
    #1
    Code:
    
     #include <stdio.h>
    
    int main()
    
    {
        int a, b;
        printf("enter a number: ");
        scanf("%d", &a);
        printf("enter another number: ");
        scanf("%d", &b);
        if (a>b);
            printf("%d is greater than %d\n", a,b);
        else
            printf("%d is less than %d\n", a,b);
        return 0;
    }
     
    my gcc keeps saying:

    file.c: In function ‘main’:
    file.c:21: error: expected expression before ‘else’


    I tried using {} before and after each print/scan command.
    I tried using:
    Code:
    else if (a<b); //instead of just "else"
    
    And I tried it with and without the ; after if and else in every combination. I just need a little direction. Thanks!
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Code:
        if (a>b); <<< --- REMOVE THIS SEMI-COLON
            printf("%d is greater than %d\n", a,b);
        else
            printf("%d is less than %d\n", a,b);
    
     
  3. Johnny5alive thread starter macrumors newbie

    Joined:
    Aug 1, 2011
    #3
    Ughh!

    Thank you! It makes sense since the ";" always goes at the end of a command.

    Did you guys have these issues when you started learning your first language? I am starting to get discouraged.

    Thanks again!

    ----------


    Or not
     
  4. lloyddean, Aug 20, 2011
    Last edited: Aug 20, 2011

    lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    Everyone - young, old, noob or seasoned veteran of multiple decades will occasionally run into this same problem.

    And it often has the same solution for them as for you!

    The difference now is that compilers like 'clang' are giving much better error statements.
     
  5. Johnny5alive thread starter macrumors newbie

    Joined:
    Aug 1, 2011
    #5
    Code:
    
    #include <stdio.h>
    
    int main()
    {
        int x, y, z;
        
        x = 0;
        do {
            printf( "Hello, world!\n" );
        } while ( x != 0 );
        
        for (x=0; x<10; x++) {
            printf("%d\n", x);
            }
        printf("enter a number\n");    
        scanf("%d\n", &x);
        printf("enter another number\n");
        scanf("%d\n", &y);
        z=x * y;
        printf("%d multiplied by %d = %d\n", x, y, z);
            
        
    }
    
    I was trying to write code from what I have learned so far from memory. When I compile and run this it all works right except after it says the first "enter a number" I have to enter two numbers for it to carry on?

    I was also wondering if the for loop has to use the increment "x++" ?
    I thought I would just be able to use x+5 or whatever increment I wanted but my compiler did not like that.

    I am just using terminal for my gcc. Do you think I should switch to a different one?

    Thanks for putting up with these silly questions it is helping!
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    I haven't written much straight C code in decades so I may be wrong with the following. But …

    Try placing

    Code:
    fflush(stdin);
    
    after the 'scanf'.

    ----------

    'x++' is the roughly the same as 'x = x + 1'

    For the simple code projects you're currently working on I'd stay with the Terminal.
     
  7. Johnny5alive thread starter macrumors newbie

    Joined:
    Aug 1, 2011
    #7
    Thanks!

    The fflush(stdin); didn't cause any noticeable change.

    Entering the increment as x = x + 5 did what I was trying to do.

    I lightly looked into Clang after you mentioned it. It seems way beyond me! I will probably be sticking to Terminal for a long while. haha.

    So, can you just sit down and write code and make a program do anything you want? Is it all code that makes Mario jump and word processors function? How does a code attach to these things? Or is the shape of Mario's hat also made from code? Ughh, you don't have to answer that. I am tired and getting ahead of myself. It is 3:00 AM here in Spain and I think it is time to stop looking at code.

    Thanks again! You have been a great help.
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    Sorry, as I said I do much more C++ than anything else these days!

    Good!

    If you're doing development under Mac OS 10.7 then 'clang' is already installed! Just substitute 'clang' for 'gcc' at the command line.

    Used to be able to but unfortunately a car accident resulting in a neck and back injury took me out of that game nine years ago. Been doing much simpler and shorter contracts project since then as I can't concentrate as well as I used to.

    The last game I worked on was this which used to on Apples site but is now cached somewhere else.
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x, y, z;
        
        x = 0;
        do
        {
            printf( "Hello, world!\n" );
        } while ( x != 0 );
        
        for ( x = 0; x < 10; x++ )
        {
            printf("%d\n", x);
        }
    
        printf("enter a number\n");    
        scanf("%d", &x);
    
        printf("enter another number\n");
        scanf("%d", &y);
    
        z = x * y;
    
        printf("%d multiplied by %d = %d\n", x, y, z);
    }
    
    Hey Johnny5alive

    Long time no 'C'!

    Took more than a glance at your code. It turns out you had extraneous "\n" at the end of both your 'scanf' statements format strings. Removing the "\n" results in working code.

    Enjoy
     
  10. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #10
    Yes. Although at some point writing all the code for it becomes tedious at which point you turn to an API where someone else has done a lot of the work for you.

    Thus you stop using just a language like C and Obj-C and instead use them in combination with Cocoa and Cocos2D and Core Graphics and other amazing time savers.
     
  11. iHutch105 macrumors member

    Joined:
    Aug 8, 2011
    #11
    You can also simplify this to x += 5.
     

Share This Page