Thread: Problem reading and analyzing 3 variables

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Problem reading and analyzing 3 variables

    I'm following a C tutorial with exercisess, and in one of them you have to make a program that when given 3 variables tells the user the biggest and smallest number... This is what I have, but I get a tonne of errors!

    Code:
    #include <stdio.h>
    void main()
    {
        int x,y,z;
        printf("Escribe tres numeros\n");
        scanf("%d %d %d", &x, &y, &z);
        if x>y && x>z && y>z {
            printf("Big numer is %d Small number is d%\n", &x,&z);
        }else if x>y && x>z && y<z{
            printf("Big numer is %d Small number is d%\n", &x,&y);
        }else if y>x && y>z && x>z{
            printf("Big numer is %d Small number is d%\n", &y,&z);
        }else if y>x && y>z && x<z{
            printf("Big numer is %d Small number is d%\n", &y,&x);
        }else if z>x && z>y && y>x{
            printf("Big numer is %d Small number is d%\n", &z,&x);
        }else
            printf("Big numer is %d Small number is d%\n", &z,&y);
        }
    }
    Errors I get include: parse error in line 7 and "return type of 'main' is not 'int' "
    Thanks

    ps I should stop using scanf...

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This:
    Code:
    if x>y && x>z && y>z {
    Needs to be this:
    Code:
    if( x>y && x>z && y>z ){
    And likewise for your other else if statements.

    ... and you're still doing that void main thing others have told you about. It may work fine but it is really frowned upon and you should get in the habit of saying int main instead.

    [edit]Almost forgot... you don't put & in front of the variables when you print them.[/edit]
    Last edited by hk_mp5kpdw; 10-04-2005 at 12:40 PM.
    "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

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    you need parens around your if statement, I dont know what tutorial you're following, but main returns an INT (as is pretty clearly specified by the error your getting) so change it to int main(void) and throw a return statement in there at the end.

    example:

    Code:
    int main(void)
    {
      if (dead_horse)
        beat;
      return 0;
    }

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Looking at your code, I can already see a half-dozen errors, and these are only related to syntax. Anyways, I re-written your stuff, as it should be corrected. I suggest you look at the comments I have left

    Code:
    #include <stdio.h>
    int main()   /*** to avoid warning, put return type of main to int  ***/
    {
        int x,y,z;
        printf("Escribe tres numeros\n");
        scanf("%d %d %d", &x, &y, &z);
        if (x>y && x>z && y>z)  /** parentheses  **/
        {
            printf("Big numer is %d Small number is %d\n", x,z);  /*** print values, not addresses ***/
        }
        else
        { 
           if (x>y && x>z && y<z)/** parentheses  **/
           {
              printf("Big numer is %d Small number is %d\n", x,y);/*** print values, not addresses ***/
           }
           else
           {
              if (y>x && y>z && x>z)/** parentheses  **/
              {
                 printf("Big numer is %d Small number is %d\n", y,z);/*** print values, not addresses ***/
              }
              else
              { 
                 if (y>x && y>z && x<z)/** parentheses  **/
                 {
                    printf("Big numer is %d Small number is %d\n",y,x);/*** print values, not addresses ***/
                 }
                 else
                 { 
                    if (z>x && z>y && y>x)/** parentheses  **/
                    {
                       printf("Big numer is %d Small number is %d\n", z,x);/*** print values, not addresses ***/
                    }
                    else
                    {
                       printf("Big numer is %d Small number is %d\n", z,y);/*** print values, not addresses ***/
                    }
                 }  /** forgottten brace  ***/
              }  /*  forgotten brace  ***/
           }   /* forgtotten brace ***/
       }
       
       return 0;   /**** to return int  ***/
    }
    Also, 'd%' in the middle of a printf statement will print 'd%'. I think what you wanted was '%d'. I took the liberty of correcting that too.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, 'd%' in the middle of a printf statement will print 'd%'.
    Actually, it will print 'd' followed by whatever special escape character translation printf() makes depending on the character immediately following the %. In this program's case it tries to convert %\ which it doesn't understand and will give you some error message related to that. To print '%' using printf() you need to tell printf() to print "%%".
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    the "d%" I could have fixed myself, but thanks for the harder stuff guys...
    Is "void main()" ever useful?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well sure! If you're looking to write illegal C code then void main() is a great way to accomplish it. Everything is useful at some time to someone.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    void main() is not necessarily "illegal". If the implementation documents it as being okay, it's then an implementation defined program, it's just no longer strictly conforming.

    If you want to write a strictly conforming C program (one that relies on no implementation defined, undefined, or unspecified behaviour), then you need to have main return an int.

    See http://homepages.tesco.net/~J.deBoyn...void-main.html for why blanket statements like "void main is illegal" are wrong.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by cwr
    See http://homepages.tesco.net/~J.deBoyn...void-main.html for why blanket statements like "void main is illegal" are wrong.
    Q - How many [...] engineers does it take to change a light bulb?
    A - None. [...] will just redefine Darkness (TM) to be the new industry standard.
    C90 was the standard, once.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Dave_Sinkula
    C90 was the standard, once.
    That page applies also to C99, the C99 standard still states that main can have some implementation defined format for its return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM