If Statements? Whatís wrong with it?

This is a discussion on If Statements? Whatís wrong with it? within the C Programming forums, part of the General Programming Boards category; Iíve done an if statement before in a previous program but it was numbers Ö Code: void process() { total=float(miles_after ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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, 11:01 AM
  4. Shorten Statements are WRONG?!
    By RoD in forum C++ Programming
    Replies: 29
    Last Post: 11-01-2002, 05:05 AM
  5. whats wrong with my if statements
    By ssjnamek in forum C++ Programming
    Replies: 8
    Last Post: 01-28-2002, 11:48 PM

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