Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

IF's in C (syntax?)

Posted on 2004-12-01
6
Medium Priority
?
321 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:ALEx604
6 Comments
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 12720929
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
 
LVL 4

Assisted Solution

by:Woodster
Woodster earned 100 total points
ID: 12721982
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
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 100 total points
ID: 12722500
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Assisted Solution

by:prakashagr
prakashagr earned 100 total points
ID: 12723645
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
 
LVL 1

Accepted Solution

by:
smiththomasg earned 100 total points
ID: 12731786
#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
 
LVL 2

Author Comment

by:ALEx604
ID: 12750135

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question