Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Johnny5alive

macrumors newbie
Original poster
Aug 1, 2011
10
0
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!
 
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!

----------

And I tried it with and without the ; after if and else in every combination. I just need a little direction. Thanks!


Or not
 
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.
 
Last edited:
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!
 
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'.

----------

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!

'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.
 
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.
 
Thanks!

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

Sorry, as I said I do much more C++ than anything else these days!

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

Good!

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.

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

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.

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.
 
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
 
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?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.