A few days into C-programmering experience: if and else

This is a discussion on A few days into C-programmering experience: if and else within the C Programming forums, part of the General Programming Boards category; Hello users! As stated in the title, I am very new to C and programming languages in general. I am ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    A few days into C-programmering experience: if and else

    Hello users!
    As stated in the title, I am very new to C and programming languages in general.

    I am currently reading various guides and such, but I've come across a syntax problem when using if and else.

    I did a little code to try it. The idea is having two integers, a and b. If a and b are equal, I want the program to calculate the product of the two. Else I want it to find the average of those numbers.

    I'm pretty sure my problem is lack of / misplaced brackets and tokens, but I cannot figure out the syntax!

    My code is
    Code:
    int main()
    {
        int a,b;
        float avg;
        float square;
        
        printf("Enter the value of a:");
        scanf("%d", &a);
        
        printf("Enter the value of b:");
        scanf("%d", &b);
     
        if (a==b)
               square = a * b;
               printf("%f", square);
                 
        else
         avg = (a+b)/2;
         printf("%f", avg);
    
    
    return 0;
    }
    Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Put braces around everything.
    Code:
        if (a==b) {
             square = a * b;
             printf("%f", square);
        } else {
             avg = (a+b)/2;
             printf("%f", avg);
        }


    Code:
        if (a==b)
               square = a * b;
    Is valid, and does what you want.

    Code:
        if (a==b)
               square = a * b;
               printf("%f", square);
    Is valid, but doesn't do what you want - the printf always happens, despite what the indentation might otherwise suggest.

    Adding an else becomes a syntax error.
    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
    Join Date
    Jan 2013
    Posts
    21
    Thank you, Salem! That was very helpful.

    Is there any alternative to printf that I might use to be able to get the result I am looking for? Perhaps cout or something?


    Never mind the underlined text. I think I might have misunderstood what you said. I'll just go try to implement the changes you pointed out

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Also look at this line:

    Code:
    avg = (a+b)/2;
    Becaus a and b are integers and 2 is an integer, this will perform integer division, so your answer will basically be an integer even though you store the result in a float. You should probably write it like this

    Code:
    avg = (float)(a + b) / 2;
    Or equivalently

    Code:
    avg = a + b;
    avg /= 2;

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Implemented the changes both of you suggested and my tiny program now works. Yay! Onwards I go into the void that is C-programming
    Thank you guys! Appreciate the quick replies

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by TobiasK View Post
    Is there any alternative to printf that I might use to be able to get the result I am looking for? Perhaps cout or something?


    In C++ cout is an alternative to printf. In C standard library, there is just printf. C++ cout uses operator overloading and polymorphic behaviour to work out how to output a value of a certain type (string, float, double, etc). Because C doesn't have a way to do this, you need to tell printf how to output something

    printf("%d", myInt);
    printf("%f", myFloat);

    So basically there's not any way for the compiler to do this for you like there is using cout in C++. Some compilers have extensions which allow you to simulate this, but using them makes your program only work on one compiler, and usually what you gain is not worth it.

    By the way, compilers will check the printf formats for you if you compile with warnings turned on. So basically if you do that, then there's basically no way you can go wrong with printf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need your experience
    By Griever in forum C Programming
    Replies: 6
    Last Post: 02-09-2009, 04:01 PM
  2. Programming Experience
    By shoutatchickens in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 04-04-2008, 01:00 PM
  3. Some experience, please
    By thebudbottle in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-27-2004, 06:45 PM
  4. 32-bit ASM or COM :: Experience
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 10-04-2002, 12:39 PM
  5. DOS experience 0 windows experience help
    By shrin2000 in forum Windows Programming
    Replies: 5
    Last Post: 07-15-2002, 02:22 AM

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