• C

IF's in C (syntax?)

Ok. trying to figure out why this doesn't work and I am lost, I am pretty sure it has to do with syntax.
(PS I will judge the correct answer with at least 50 points when I have it, if there is a big change I'll give more points, Also  if you include other common errors newbies make with "if statements" I'll increase the points as well)

int main(int argc, char *argv[])
{
  printf( "This is a Hello World Program\n ");
int interger1 ;
int interger2 ;
int interger3 ;
int sum ;
printf( "\n Enter a number: \n");
scanf( "%d" , &interger1 ) ;
printf( "\n Enter another number: \n");
scanf( "%d" , &interger2 ) ;
printf( "\n Enter how much difference is allowed:\n");
scanf( "%d" , &interger3 ) ;
if ( interger1 <= interger2 ) {
sum = interger1 - interger2 ;
}
else if ( interger1 > interger2 ){
sum = interger2 - interger1 ;
}
if ( interger3 >= sum ){
printf("\n\nIt's to much of a difference\n\n");}
else if ( interger3 < sum ){
printf("\n\nThis is not to much of a difference\n\n");}
return EXIT_SUCCESS;
}
LVL 2
ALEx604Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:
Hi Alex,

It actually looks pretty rational.  I would indent a bit differently, but that's for human readability.

When you say it doesn't work, what do you mean?  Is there a compilation error/warning?  Funny results when you run it?

Obviously, there are some things that you haven't put in the post, like which files are #included.


Kent
0
WoodsterCommented:
As Kent mentioned, what error are you actually getting?

Firstly, you don't really need the else if statements that you have listed

if ( interger1 <= interger2 )
{
  sum = interger1 - interger2 ;
}
else // if ( interger1 > interger2 ) will always succeed if we get to this else clause
{
  sum = interger2 - interger1 ;
}

and the same here also:

if ( interger3 >= sum )
{
  printf("\n\nIt's to much of a difference\n\n");
}
else // if ( interger3 < sum ) will always succeed if we get to this else clause
{
  printf("\n\nThis is not to much of a difference\n\n");
}

As far as mistakes with if statements made by beginners, the main one I have found is the use of = as opposed to ==

int a = 2;
if (a = 1)
{
  // We will ALWAYS get to here.  The if statement above assigned the value of 1 to the int variable a.
  // a is now a non zero value which is in C / C++ is equivalent to true so the if statement succeeds.
}

if (a==1)

This is the correct statement if you want to test if the value of a is equal to 1.
0
brettmjohnsonCommented:
It looks like you have the sense of your test backward:

if ( interger1 <= interger2 )
{
 sum = interger1 - interger2 ;
}

will result in sum being a negative number (or zero).
The following will result in sum being positive (or zero):

if ( interger1 > interger2 )
{
 sum = interger1 - interger2 ;
} else {
 sum = interger2 - interger1 ;
}

Alternative syntax would be as a ternary:

sum = ( interger1 > interger2 ) ? interger1 - interger2 : interger2 - interger1 ;

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

prakashagrCommented:
As per u'r code the result is comming correct , i.e sum is always -ve number ,
for ex if u assign 6 and 8 (or 8 and 6) to interger1,interger2 respectivly u always get sum is -2

if u gave -2 or any +ve number to interger3 it will print
It's to much of a difference

of if u give values -3 or -ve numbers(less than -2)  it will print
This is not to much of a difference


So wat r the error u'r getting, wat u want from this code, for +ve values u have to change the code as
brettmjohnson  mentioned above


#include <stdio.h>
int main(int argc, char *argv[])
{
int interger1 ;
int interger2 ;
int interger3 ;
int sum ;
printf( "This is a Hello World Program\n ");
printf( "\n Enter a number: \n");
scanf( "%d" , &interger1 ) ;
printf( "\n Enter another number: \n");
scanf( "%d" , &interger2 ) ;
printf( "\n Enter how much difference is allowed:\n");
scanf( "%d" , &interger3 ) ;
if ( interger1 <= interger2 ) {
sum = interger1 - interger2 ;
}
else //if ( interger1 > interger2 ){
{
sum = interger2 - interger1 ;
}
if ( interger3 >= sum ){
printf("\n\nIt's to much of a difference\n\n");}
else // if ( interger3 < sum ){
{
printf("\n\nThis is not to much of a difference\n\n");}
//return EXIT_SUCCESS;
}



Prakash

0
smiththomasgCommented:
#include <stdio.h>
/* ****** Needed for abs() function */
#include <stdlib.h>

int main(int argc, char *argv[])
{
int interger1;
int interger2;
int interger3;
int sum ;
printf( "This is a Hello World Program\n ");
printf( "\n Enter a number: \n");
scanf( "%d" , &interger1 ) ;
printf( "\n Enter another number: \n");
scanf( "%d" , &interger2 ) ;
printf( "\n Enter how much difference is allowed:\n");
scanf( "%d" , &interger3 ) ;
/* ****** Determine the difference */
  sum=abs(interger1-interger2);
/* ****** You had >= */
  if ( interger3 < sum )
        printf("\n\nIt's to much of a difference\n\n");
  else  printf("\n\nThis is not to much of a difference\n\n");
/* ****** You exit a program and return from a subroutine or function */
  exit(0);
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ALEx604Author Commented:

I will review each of the above and get back to you with any error codes including the orginial.

( Points will take some time I think since I would like to increase it to at least 125 )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.