Click here to Skip to main content
12,356,616 members (69,662 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C
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 7-Sep-12 17:18pm
Edited 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 8 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100