Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wanted to solve equations by taking the equation from user instead of taking only coefficient how do i collect the coefficents from the equation

What I have tried:

C++
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string s;
   cin>>s;
   for(int i=0;i<strlen(s);i++)
   {
       if()
   }
    return 0;
}
Posted
Updated 17-Mar-16 7:51am
v2
Comments
jeron1 17-Mar-16 13:19pm    
Have you tried it on paper? If you're going to show code, try to show code that can compile.

1 solution

try, for instance
C++
#include <iostream>
using namespace std;

int main()
{

 double a,b;

 cout << "the equation is 'ax+b = 0'" << endl;
 cout << "please enter the value of 'a'" << endl;
 cin >> a;
 cout << "now, please enter the value of 'b'" << endl;
 cin >> b;

 if ( a != 0.0)
   cout << "the value of 'x' is " << (-b/a) << endl;
 else
   cout << "sorry, unable to find out the value of 'x'" << endl;

}


[update]
If you need to accept from the user a single string like, for instance
C++
23.75 x + 10.18 = 0

The you have to build a (simple) parser in order to
  • formally validate the equation
  • extract the values a = 23.75, b = 10.18
.
Google is you friend[^].
[/update]
 
Share this answer
 
v2
Comments
Member 12270086 17-Mar-16 13:32pm    
i need to take ax+b=0 equation as a input not a and b...
Patrice T 17-Mar-16 20:01pm    
You also need to work :)
Matt T Heffron 17-Mar-16 13:52pm    
+5!
CPallini 17-Mar-16 14:01pm    
Thank you.

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