Some problems

This is a discussion on Some problems within the C Programming forums, part of the General Programming Boards category; Hello, i tried do some excerises from a book i have, one exercise says to create a program tha you ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    Some problems

    Hello, i tried do some excerises from a book i have, one exercise says to create a program tha you will enter 3 numbers and it will print the biggest and then the 2s biggest and the 3th biggest
    for example if you add 8 9 1 it should print
    8
    9
    1
    so i did this:
    Code:
    #include <stdio.h>
    
    main()
    {
            int ga, ga2, ga3;
    
            printf("Write 3 numbers: \n");
            scanf("%i %i %i", &ga, &ga2, &ga3);
            if (ga> ga2 > ga3)
            {
                    printf("The numbers are: %i %i %i\n", ga, ga2, ga3);
            }
            if (ga3 > ga2 > ga)
            {
                    printf("The  numbers are: %i %i %i\n", ga3, ga2, ga);
            }
            if (ga < ga2 <ga3)
            {
                    printf("The numbers are: %i %i %i\n", ga2, ga, ga3);
            }
    }
    but it doesn't work


    in the same book i also got an exercise to create a program that when you insert a number betwwen 1 - 7 it will print the days of the week, i could do that by easy with case and swtch but it need if-else
    so i did this:
    Code:
    #include <stdio.h>
    /* Apo askisi 6 selida 81*/
    main()
    {
            int numb;
            printf("Write a number: \n");
            scanf("%d", &numb);
            if (numb = '1')
            {
                    printf("Deutera\n");/*means monday*/
            }
            else
            {
            if(numb = '2')
            {
                    printf("Triti\n");/*means thirday*/
            }
            }
            else
            {
            if(numb = '3')
            {
                    printf("Tetarti\n");/*means thirsday*/
            }
            }
            else
            {
            if(numb = '4')
            {
                    printf("Pempti\n");/*means thusday*/
            }
            }
            else
            {
            if(numb = '5')
            {
                    printf("Paraskeui\n");/*means friday*/
            }
            }
            else
            {
            if(numb = '6')
            {
                    printf("Sabbato\n");/*means saturday*/
            }
            }
            else
            {
     {
            if(numb = '6')
            {
                    printf("Kiriaki\n");/*means sunday*/
            }
           }
    }
    doens't work thoyght
    please help me

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Perhaps you should go back and read up on the logical operators. Hell, go back and read up on ALL the operators covered so far. Because you seem to have forgotten the very basics:

    = is for assignment.
    == is for equality.

    AND a few otherthings. Do it now OR you'll have severe problems in everything you do related to programming.

    Quzah.
    Last edited by quzah; 12-26-2004 at 08:53 AM. Reason: Fun with color, AND a few other things...
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if (ga> ga2 > ga3)
    These tests don't work like you would expect. It's the same as:
    Code:
    int a = ga > ga2;
    if (a > ga3)
    ga3 is tested against a boolean value, which isn't what you want, I would wager. The correct test would be:
    Code:
    if (ga > ga2 && ga2 > ga3)
    >if (numb = '1')
    = is assignment, == is comparison.
    My best code is written with the delete key.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Use a switch for the second code , it'll make it much easier.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    ok, will try again

    hey brain-cell
    you must read the whole topic(don't be bored)
    i could do that by easy with case and swtch but it need if-else

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Use a switch for the second code , it'll make it much easier.
    Quote Originally Posted by cogeek
    i could do that by easy with case and swtch but it need if-else
    Though, a switch still isn't an optimal solution. A table based approach is better, but these are beginner's problems, so I didn't introduce it.
    My best code is written with the delete key.

  7. #7
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by cogeek
    Hello, i tried do some exercises from a book i have, one exercise says to create a program tha you will enter 3 numbers and it will print the biggest and then the 2s biggest and the 3th biggest
    for example if you add 8 9 1 it should print
    8
    9
    1
    Certainely not. You probably meant :
    9
    8
    1
    so i did this:
    Code:
            int ga, ga2, ga3;
    
            scanf("%i %i %i", &ga, &ga2, &ga3);
            if (ga> ga2 > ga3)
            {
    but it doesn't work
    C expressions doesn't work like that. You must use two operands and a comparision operator in-between:
    Code:
    operand1 operator operand2
    For example:
    Code:
            if (ga> ga2)
    If you have several expressions, you have to combine them using the same pattern :
    Code:
            if (ga> ga2  && ga2 > ga3)
    in the same book i also got an exercise to create a program that when you insert a number betwwen 1 - 7 it will print the days of the week, i could do that by easy with case and swtch but it need if-else
    so i did this:
    Code:
            int numb;
            scanf("%d", &numb);
            if (numb = '1')
    doens't work thoyght
    please help me
    You are confusing the digit characters '1', '2' etc. and the numerical values 1, 2 ... Remove the ' '. Also, the comparision operator is '==' and not '=' (assignment).

    (BTW, you have an [edit]) button to correct the typos on your post... I know that English is not your first language, it's not mine either).
    Last edited by Emmanuel Delaha; 12-26-2004 at 09:06 AM.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    hey when i added 2 == i got the same error when compiling:
    daysif.c: In function `main':
    daysif.c:19: parse error before "else"
    ok, i will try without the 'x'

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Prelude
    >Use a switch for the second code , it'll make it much easier.

    Though, a switch still isn't an optimal solution. A table based approach is better, but these are beginner's problems, so I didn't introduce it.
    And it was precisely demanded to use 'if' rather than 'switch'. (Well, I guess : )
    i could do that by easy with case and swtch but it need if-else
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    hey , i delete the ' and added == instead = but still nothing in the days program

  11. #11
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by cogeek
    hey when i added 2 == i got the same error when compiling:
    daysif.c: In function `main':
    daysif.c:19: parse error before "else"
    ok, i will try without the 'x'
    You have too many '}'. You should work on your indentation (or use an indenter). It makes things easier.
    Emmanuel Delahaye

    "C is a sharp tool"

  12. #12
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by cogeek
    hey , i delete the ' and added == instead = but still nothing in the days program
    This is meaningless. You must copy and paste the error messages from you development system. We are not psychic.
    Emmanuel Delahaye

    "C is a sharp tool"

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    hey emmanuel:
    you must not be psychic just read the whole TOPIC:
    Today, 07:35 PM
    hey when i added 2 == i got the same error when compiling:
    daysif.c: In function `main':
    daysif.c:19: parse error before "else"
    ok, i will try without the 'x'
    and i get the same eroors without '

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Welcome to your fifth red. There's a reason you get those. That reason is: YOU DON'T LISTEN. Why on earth should we bother helping you if you don't listen?

    So which ' did you remove? You have a whole bunch in your code. That's the reason people tell you to post your latest attempt. I'm done helping you. You have no desire to learn, nor do you listen. Good bye.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >you must not be psychic just read the whole TOPIC
    Maybe you should read the whole topic:
    Quote Originally Posted by Emmanuel Delaha
    You have too many '}'.
    Your attitude isn't endearing you to us, so it would be a good idea to think before you type.
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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