Hello,
I try to solve an equation. I wtrote this code that compiles with no problem but when I run it it gives me no result. Can anyone help me?
Thanks

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a,b,c;
    cout<<"Introduce a,b,c\n";
    cin>>a>>b>>c;
    if(a=0)
    if(b=0)
    if(c=0)
    {
           cout<<"The ecuation has an infinity of soultion";
           }
    if(a=0)
    if(b=0)
    if(c !=0)
    {
            cout<<"The ecuation has no solution";
            }
    if(a=0)
    if(b!=0)
    if(c!=0)
    {
            double x;
            x=-c/b;
            cout<<" The value of x is: "<<x<<endl;
}
if(a!=0)
{
        double delta;
        delta = (b*b) -4*(a*c);
        if(delta>0)
        {
                   double x_1, x_2;
                   x_1=(-b+sqrt(delta))/2*a;
                   x_2=(-b-sqrt(delta))/2*a;
                   cout<<" The value of x is = " <<x_1 <<" or" << " "<<x_2;
                   }
        if(delta<0)
        {        
                 double x_1, x_2;
              x_1=(-b+sqrt(-delta))/2*a;
                   x_2=(-b-sqrt(-delta))/2*a;
                   cout<<"  The value of x is = " <<x_1 <<" or" << " "<<x_2;
                   }
                   }
                   system("pause");
                   return 0;
                   }

Your if statements are wrong.

It should be

if (condition) {statement;}

While you have

if (condition)
if (condition)
if (condition)

Plus a = 0 is an assignment(a now equals 0)....You want

a == 0 which is a comparison and yields true or false.

Edited 6 Years Ago by gerard4143: n/a

#include<iostream>
#include<cmath>

using namespace std;
int main()
{

   double EqR,a,b,c,delta;

   EqR=a=b=c=delta=0;

   cout<<"\nEnter the Value for a:";
   cin>>a;

   cout<<"\nEnter the Value for b:";
   cin>>b;

   cout<<"\nEnter the Value for c:";
   cin>>c;

   if(a==0 && b==0 && c==0)
   {
    cout<<"\nThe equation has an infinity solution";
   }

       else if(a==0&& b!=0 && c!=0)
	   {    
		   double x; 
		   x=-c/b;          
		   cout<<" The value of x is: "<<x<<endl;
         }
       else if(a!=0)
	   {
			   
			   delta = (b*b) -4*(a*c);    
			   if(delta>0)  
			   {       
				   double x_1, x_2;   
				   x_1=(-b+sqrt(delta))/2*a;
				   x_2=(-b-sqrt(delta))/2*a;
				   cout<<" The value of x is = " <<x_1 <<" or" << " "<<x_2<<endl;
			   }  
			   else if(delta<0)  
			   {
				   double x_1, x_2;
				   x_1=(-b+sqrt(-delta))/2*a;
				   x_2=(-b-sqrt(-delta))/2*a;
				   cout<<"  The value of x is = " <<x_1 <<" or" << " "<<x_2<<endl;               
			   }
	   }
  return 0;
}

You need to clear your understanding of conditional loops.You also need to cover logical operators. This code is working now but needs modification for equations like
x^2 + 4x + 4 = 0

hope this helps

Hello,
I try to solve an equation. I wrote this code that compiles with no problem but when I run it it gives me no result. Can anyone help me?
Thanks

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a,b,c;
    cout<<"Introduce a,b,c\n";
    cin>>a>>b>>c;
    if(a=0)
    if(b=0)
    if(c=0)
    {
           cout<<"The ecuation has an infinity of soultion";
           }
    if(a=0)
    if(b=0)
    if(c !=0)
    {
            cout<<"The ecuation has no solution";
            }
    if(a=0)
    if(b!=0)
    if(c!=0)
    {
            double x;
            x=-c/b;
            cout<<" The value of x is: "<<x<<endl;
}
if(a!=0)
{
        double delta;
        delta = (b*b) -4*(a*c);
        if(delta>0)
        {
                   double x_1, x_2;
                   x_1=(-b+sqrt(delta))/2*a;
                   x_2=(-b-sqrt(delta))/2*a;
                   cout<<" The value of x is = " <<x_1 <<" or" << " "<<x_2;
                   }
        if(delta<0)
        {        
                 double x_1, x_2;
              x_1=(-b+sqrt(-delta))/2*a;
                   x_2=(-b-sqrt(-delta))/2*a;
                   cout<<"  The value of x is = " <<x_1 <<" or" << " "<<x_2;
                   }
                   }
                   system("pause");
                   return 0;
                   }

Half of the solution is working but is only one problem.
At the end of the code where is

if(a!=0)
if((b==0) || (b!=0))
if((c==0) || (c!=0))

{
        double delta;
        delta = (b*b) -4*(a*c);
        if(delta>0)
        {
                   double x_1, x_2;
                   x_1=(-b+sqrt(delta))/2*a;
                   x_2=(-b-sqrt(delta))/2*a;
                   cout<<" The value of x is = " <<x_1 <<" or" << " "<<x_2<<endl;
                   }
        if(delta<0)
        {        
                 double x_1, x_2;
              x_1=(-b+sqrt(-delta))/2*a;
                   x_2=(-b-sqrt(-delta))/2*a;
                   cout<<"  The value of x is = " <<x_1 <<" or" << " "<<x_2<<endl;
                   }
                   }
                   system("pause");
                   return 0;
                   }

if I put a non zero value to a and I put b =0 and c=0 the program doesn't return me a solution.
If I put non zero values for a, b, c it works fine

Use the logical OR operator, which is "||" instead of "&&"

"&&" is the AND operator

(TRUE && FALSE && TRUE) evaluates to (FALSE)
(TRUE || FALSE || TRUE) evaluates to (TRUE)

Edit: A poster above me already mentioned this, but you should probably read up on the logical operators in general.

Edited 6 Years Ago by Unimportant: n/a

As you see I am using the or operator but it still doesn't work if a=8, b=0,c=0

This is what you posted

if(a!=0)
if((b==0) || (b!=0))
if((c==0) || (c!=0))

What this means is:
if A does not equal 0, execute: if B does not equal 0 OR if B DOES equal zero (no matter what): execute: if C equals or does not equal 0 (no matter what) execute:
// the rest of what you did

Basically all of your code is just dependent on whether or not a is 0, and the rest is totally off.

You don't know how to use if statements, nor do you know what logical operators do.

What you want is just this:

if( a != 0 || b != 0 || c != 0 ) { // if A OR B OR C are not equal to 0
  // do stuff
}

if statements will execute up to 1 line with no curly brackets, like so:

if(true) foo(); // calls foo()
if(true)
  foo(); // calls foo()
if(true)
if(false) { // is called if(true), but will never pass, whatever is inside of here will never execute
  std::cout << "*whispers* Hello World! ...sniffle" << std::endl;
}

Edited 6 Years Ago by Unimportant: n/a

This article has been dead for over six months. Start a new discussion instead.