Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Yes! The frist time try "if-else". Like I said above, here is the code:

#include<stdio.h>
main()
   {
   int a,b,x;
   printf("\n Input a: ");
   scanf("%d",&a);
   printf("\n Input b: ");
   scanf("%d",&b);
   if (a=0)
      {
      if (b=0)
         printf("\nThe equation has unlimited result");
      else 
         printf("\nThe equation has no result");
      getch();
      }
   else
      {
      x=-b/a;
      printf("\nThe equation has only 1 result x= %d", x);
      getch();
      }
   }


The problem is: I have compiled successfully in Dev-C++. But when I run it, the program crash after input 2 numbers (a and b) (The program has stoped working...).


[edit]Indentation corrected so the flow is clear - OriginalGriff[/edit]
Posted
Updated 7-Sep-12 21:11pm
v2
Comments
enhzflep 7-Sep-12 23:46pm    
Hint:


#include <stdio.h>
int main()
{
int a=2, b=10;
if (a=b)
printf("Oops! Think I stuffed-up my comparison. I should've used == instead of =\n");
else
printf("how the hell did program execution get here?\n");
}
Minh Hikari 8-Sep-12 1:10am    
yeah! I think i unserstand what you mean...:D Thank you very much!!! But i still have a question: what is the different between "=" and "=="?
enhzflep 8-Sep-12 1:13am    
Well, = is an assignment operator, while == is a comparison operator.

a = 5; - assignment (Set a to 5)
if (a==5) - comparison (Check if a is 5)
Minh Hikari 8-Sep-12 1:19am    
ok! I understand clearly now..^^ Thank you very much!!!^^

1 solution

Make a habit of doing comparisons like this :
if (0 == a)
if you mistakenly do this :
if (0 = a)
compilation will fail because 0 cannot be assigned.
This kind of error is very common and even professional programmers still make this error sometimes, but the above trick helps avoid having to look for this kind of error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900