What's wrong with my if else then function? It complies fine (Dev C++) but it doesn't function properly. It displays all the messages together.

void data(void)
{
cout<<"How many hours did you work: ";
cin>> total_hours;


cout<<"How many days did you work: ";
cin>> days;

return;
}
void process
{
income_distrubution== ((income/ total_hours));
income= (days * 50);
return;
}

Void result
{
if(income> total_hours)
{
cout << "Error.";
}
if (income< (total_hours *.30))
{
cout<< "You need to work more!." << endl;
}
if((total_hours >= 0 && days >= 0 ))
{
cout<<"Your income distribution : "<< income_distrubution<<" % "<<endl;
}
else
{
cout<<"The number of hours/days entered must be greater than 0."<<endl;
}

Edit/Delete Message

Try to use an else-if ladder:

void result
{
  if(income> total_hours)
  {
    cout << "Error.";
  }
  else if (income< (total_hours *.30))
  {
    cout << "You need to work more!." << endl;
  }
  else if((total_hours >= 0 && days >= 0 ))
  {
    cout << "Your income distribution : "<< income_distrubution<<" % " << endl;
  }
  else
  {
    cout << "The number of hours/days entered must be greater than 0." << endl;
  }
} // you forgot an ending brace here

Also: Wrap your code between code tags next time

Edited 3 Years Ago by happygeek: fixed formatting

if(condition) { do this }
else if(another condition) { do this }
else if(another condition) { do this }
else { do default }

I actually never understood why you would need to use an else-if... it seems that you should only ever hit one of the "else if"s , so if you just had several "if"s you would only hit one of them too, right?

To the OP:
This line is not making any sense: income_distrubution== ((income/ total_hours)); , notice the double '=', this expression will be evaluated, and the result will be ignored, this means that the result is not assigned to the variable income_distribution.

Could you just post all of your code (and your actual assignment)? There are some important things missing for us to be able to help you better.
BTW, by just looking at the code you posted, I guess you've multiple global variables, which is a bad idea, because all your functions are relying on data outside themselves, what if a function accidentally changes a variable's value? (I agree that the use of one or more global variables can provide a convenient solution in some cases, but for most problems you probably want to avoid using them).

I actually never understood why you would need to use an else-if... it seems that you should only ever hit one of the "else if"s , so if you just had several "if"s you would only hit one of them too, right?

Anyway, if you just write if instead of else-if, then the condition will always be evaluated, and when the result yields true the code associated with that if will be executed, this is not the case if you use an else-if ladder. (I should have to see his assignment to evaluate whether my proposal was good in this case, could be that you're correct here).
Technically else-if is just something like this, I changed the indenting to actually make clear what it really is:

if( [I]expression[/I] )
{
    /* a chunk of code here */
}
else
{
   if( [I]expression[/I] )
   {
        /* a chunk of code here */
   }
   else
   {
       if( [I]expression[/I] )
       {
           /* a chunk of code here */
       }
       else
       {
           /* a chunk of code here */
       }
   }
}

Which does exactly the same as:

if( [I]expression[/I] )
{
    /* a chunk of code here */
}
else if( [I]expression[/I] )
{
    /* a chunk of code here */
}
else if( [I]expression[/I] )
{
    /* a chunk of code here */
}
else
{
    /* a chunk of code here */
}

, which is not the same as:

if( [I]expression[/I] )
{
    /* a chunk of code here */
}
if( [I]expression[/I] )
{
    /* a chunk of code here */
}
if( [I]expression[/I] )
{
    /* a chunk of code here */
}
else
{
    /* a chunk of code here */
}

In the last one, the expression in each if is always evaluated, so when you've for example two ifs, where the expression results in a non-zero value, the code associated with both ifs is executed.
In an else-if-ladder this isn't the case, the first expression which is evaluated to a non-zero value causes the code associated with that if to be executed, bypassing all other ifs when the code associated with that if was executed.

This article has been dead for over six months. Start a new discussion instead.