Thread: If Statements? What’s wrong with it?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    If Statements? What’s wrong with it?

    I’ve done an if statement before in a previous program but it was numbers …

    Code:
    void process()
    {
         total=float(miles_after - miles_before)*0.42f;
         if (total>=1000);
         {
         total=total+10.84;
         }
    
    }
    But now I’m trying to do it with text and it doesn’t seem to be working. When I run the program the text appears weather my statement is true or flase.

    Code:
    void output()
    {
         printf("Your new balance is %2.f\n",updated_current);
         if(updated_current<0);
         {
         printf("We would like to make you aware you have entered your over draft.");
         }
    }
    Help much appreciated!

    Chris

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Lose the semi-colon:
    Code:
    if(updated_current<0);

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    Quote Originally Posted by rags_to_riches View Post
    Lose the semi-colon:
    Code:
    if(updated_current<0);
    Simple as that ...

    I find sometimes thats all you need someone else to spot the silly mistakes. Thanks man

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you indented like this:
    Code:
    void output() {
         printf("Your new balance is %2.f\n",updated_current);
         if(updated_current<0)  {
               printf("We would like to make you aware you have entered your over draft.");
         }
    }
    Instead of like this:
    Code:
    void output()
    {
         printf("Your new balance is %2.f\n",updated_current);
         if(updated_current<0);
         {
         printf("We would like to make you aware you have entered your over draft.");
         }
    }
    Mistakes like that will be harder to make, easier to spot, AND your code will be more readable for everyone including yourself! If you want to add whitespace, use a blank line instead of placing { by itself -- which will usually be the least appropriate place for more whitespace anyway.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is possible to do it this way, too:
    Code:
    void output()
    {
         printf("Your new balance is %2.f\n", updated_current);
         if (updated_current < 0)
         {
               printf("We would like to make you aware you have entered your over draft.");
         }
    }
    Or even
    Code:
    void output()
    {
         printf("Your new balance is %2.f\n", updated_current);
         if (updated_current < 0)
               printf("We would like to make you aware you have entered your over draft.");
    }
    Also, I'd like to say: lose the globals. Pass by arguments instead. It makes debugging much easier.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  2. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Shorten Statements are WRONG?!
    By RoD in forum C++ Programming
    Replies: 29
    Last Post: 11-01-2002, 06:05 AM
  5. whats wrong with my if statements
    By ssjnamek in forum C++ Programming
    Replies: 8
    Last Post: 01-29-2002, 12:48 AM