Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 22nd, 2013, 07:30 AM
 ayberkeve
Registered User

Join Date: Apr 2012
Posts: 14
Time spent in forums: 7 h 39 m
Reputation Power: 0
Relations between numbers

Hi !
I am having trouble with this code. It gives error where the "else" statement begins. The code should deliver the relations between integer-values a,b,c. For example : a<=b=c or a<b<c
I'd be glad if you could help !
Thanks
Code:
```  #include <stdio.h>
#include <stdlib.h>

int main()
{
int a=0,b=0,c=0;
printf("Enter three numbers:\n");
printf("a:");
scanf("%d",&a);
printf("b:");
scanf("%d",&b);
printf("c:");
scanf("%d",&c);

if (a<=b) {
if(b<=c) {
if(a==b && b==c) {
printf("%d=%d=%d",a,b,c);}
else if (a<b && b==c)  {
printf("%d<%d=%d",a,b,c);}
else if (a=b && b<c);{
printf("%d=%d<%d",a,b,c);}
else {
printf("%d<%d<%d",a,b,c);
}
}
else if (c<=a) {
if (c=a && a<b) {
printf("%d=%d<%d",c,a,b);}
else if (c<a && a=b) {
printf("%d<%d=%d",c,a,b);}
else {
printf("%d<%d<%d",c,a,b);}
}

}

return 0;

}

```

#2
September 22nd, 2013, 07:37 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
> else if (a=b && b<c);
1. You're using = where you should be using == (not the only place you do this).
2. You have a mis-placed ; at the end of the line.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
September 22nd, 2013, 07:53 AM
 ayberkeve
Registered User

Join Date: Apr 2012
Posts: 14
Time spent in forums: 7 h 39 m
Reputation Power: 0
Thank you so much ! I ve been overlooking these small errors.

#4
September 22nd, 2013, 03:37 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,165
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 34 m 16 sec
Reputation Power: 455

In my case, it took some time to understand compiler messages. So true while learning the language. Let's fix the misplaced semi-colon and compile with warnings enabled.
Code:
```\$ gcc -Wall ./c.c
./c.c: In function 'main':
./c.c:20:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
./c.c:27:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
./c.c:29:26: error: lvalue required as left operand of assignment
\$ ```
"warning: suggest parentheses around assignment used as truth value" certainly worth investigating, and you did by writing to this forum.

a=b /* assignment expression */
a==b /* comparison expression */

"error: lvalue required as left operand of assignment" say what???
This is a combination of precedence trouble and the incorrect use of an assignment where comparison was intended.
Assignment has the lowest precedence of any of the operations in that expression, as if it were parenthesized
(c<a && a) = (b)
in other words, store as the int that results from (c<a && a) the value indicated by the variable b. That makes no sense. If you wanted to compute an address you'd have to cast and dereference and take your segmentation fault but we won't go there.
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : September 22nd, 2013 at 03:43 PM.

#5
September 22nd, 2013, 10:01 PM
 gothrog
Micro Junky

Join Date: Jul 2002
Location: NJ
Posts: 93
Time spent in forums: 6 h 25 m 11 sec
Reputation Power: 12
Your code has an extra ; in the 'else if'

Code:
```else if (a=b && b<c);{
printf("%d=%d<%d",a,b,c);}
else {```
__________________
The Rog

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Relations between numbers