I have a problem with my program

I'm new to this website, so don't scold me if I posted this in the wrong place.
So basically, I'm making this problem that solves equations, like 5x+45=21+7x (Solving for the value of "x"). I am receiving no more errors in my code. The problem is, that after I input the equation, it doesn't solve it. It only allows me to infinitely press "Enter". And please take in account, I'm still new to C++ programming, so don't give me complicated solutions.
Here's the source code:
#include <iostream>
using namespace std;
char firstEquation[20]; //input first half
char secondEquation[20]; // input second half
double XfirstEquation[20]; //used so I can loop through and find x to //replace it
double XsecondEquation[20]; // same as above
int a; //for loop
int i; // for loop
double x; // this is the product of the program
int bob; // (sorry bad name) this counts how many loops it took to find x
double multi; // converts x and the number before it, like 5x would become 5 * x
double multi2;// same as above, but for other side
int main()
{

cout << "Welcome to the equation solver!\n In v1, we only can solve for one variable. Make sure that variable is \"x\".\n You cannot type in your equation as, for example, 5+x=3+2x. You only enter 5+x for the first equation, then 3+2x for the second";
cout << "\n Enter the first equation *No Spaces*\n";
cin >> firstEquation;
cout << "Now the second half *No Spaces*\n";
cin >> secondEquation; //after this, the problem I described occurs
cout << "Now let me work my algorithmetic magic!\n";
for ( i = 0; i < 20; i++){
firstEquation[i] = XfirstEquation[i];//first half of equation gets fixed

if (XfirstEquation[i] == 'x'){
multi = XfirstEquation[i-1]*x;
XfirstEquation[i] = 0;
XfirstEquation[i-1] = 0;
XfirstEquation[i-1] = multi;
cout << "bob";
}
}
for (a = 0; a < 20; a++){
secondEquation[a] = XsecondEquation[a]; //second half gets fixed
if (XsecondEquation[a] == 'x'){
multi2 = XsecondEquation[a-1]*x;
XsecondEquation[a] = 0;
XsecondEquation[a-1] = 0;
XsecondEquation[a-1]= multi2;
}
}
//next comes the part where we loop through possible scenarios to make both //sides equivalent. x is the same number
for (x = -100.0; XfirstEquation != XsecondEquation; x += 0.1){
bob += 1; //this counts number of loops
if (XfirstEquation == XsecondEquation){
cout << "It took " << bob << "many tries, to find that:\n x = " << x << ".";
}
}



}


Last edited on
I'm new to this website, so don't scold me if I posted this in the wrong place.


Right place, just one thing missing. The code is very hard to read, please edit your post and use code tags for the code to make it more readable - http://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
char firstEquation[20]; //input first half
char secondEquation[20]; // input second half
double XfirstEquation[20]; //used so I can loop through and find x to //replace it
double XsecondEquation[20]; // same as above
int a; //for loop
int i; // for loop
double x; // this is the product of the program
int bob; // (sorry bad name) this counts how many loops it took to find x
double multi; // converts x and the number before it, like 5x would become 5 * x
double multi2;// same as above, but for other side
int main()
{

cout << "Welcome to the equation solver!\n In v1, we only can solve for one variable. Make sure
 that variable is \"x\".\n You cannot type in your equation as, for example, 5+x=3+2x. 
You only enter 5+x for the first equation, then 3+2x for the second";
cout << "\n Enter the first equation *No Spaces*\n";
cin >> firstEquation;
cout << "Now the second half *No Spaces*\n";
cin >> secondEquation; //after this, the problem I described occurs
cout << "Now let me work my algorithmetic magic!\n";
for ( i = 0; i < 20; i++){
firstEquation[i] = XfirstEquation[i];//first half of equation gets fixed

if (XfirstEquation[i] == 'x'){
multi = XfirstEquation[i-1]*x;
XfirstEquation[i] = 0;
XfirstEquation[i-1] = 0;
XfirstEquation[i-1] = multi;
cout << "bob";
}
}
for (a = 0; a < 20; a++){
secondEquation[a] = XsecondEquation[a]; //second half gets fixed
if (XsecondEquation[a] == 'x'){
multi2 = XsecondEquation[a-1]*x;
XsecondEquation[a] = 0;
XsecondEquation[a-1] = 0;
XsecondEquation[a-1]= multi2;
}
}
//next comes the part where we loop through possible scenarios to make both //sides equivalent. x is the same number
for (x = -100.0; XfirstEquation != XsecondEquation; x += 0.1){
bob += 1; //this counts number of loops
if (XfirstEquation == XsecondEquation){
cout << "It took " << bob << "many tries, to find that:\n x = " << x << ".";
}
}



}
Last edited on
firstEquation[i] = XfirstEquation[i]; overwrites the equation you typed in.
Can you add numbers in string format? If you can, I will remove that part.
xfirstEquation is type double
firstEquation is type char
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>

// parse a polynomial of the form "ax + b" where a and b have no sign prefix
// for instance, with poly == "7.2x - 6", sets a to 7.2, b to 6.0 and op to '-'
// return true if successfully parsed
bool parse( const std::string& poly, double& a, double& b, char& op )
{
    // step 1: locate the position of 'x'
    // http://en.cppreference.com/w/cpp/string/basic_string/find
    std::size_t pos_x = poly.find('x') ;
    if( pos_x == std::string::npos ) return false ; // no 'x'

    // step 2: locate the position of the operator
    // http://en.cppreference.com/w/cpp/string/basic_string/find_first_of
    std::size_t pos_op = poly.find_first_of( "+-*/" ) ;
    if( pos_op == std::string::npos ) return false ; // no operator

    // step 3: set the values
    // http://en.cppreference.com/w/cpp/string/basic_string/stof
    try
    {
        a = std::stod( poly.substr( 0, pos_x ) ) ; // everything before the 'x', converted to double
        b = std::stod( poly.substr(pos_op+1) ) ; // everything after the operator, converted to double
    }
    catch( const std::exception& ) { return false ; } // error converting to double

    op = poly[pos_op] ; // the operator
    return true ;
}

int main()
{
    const std::string poly = " 23.45x * 6.7" ;
    double a ;
    double b ;
    char op ;

    if( parse( poly, a, b, op ) ) std::cout << "a == " << a << ", b == " << b << ", op == '" << op << "'\n" ;
}

http://coliru.stacked-crooked.com/a/36aaf6a4c4e86a51

Take it up from there.
I just learned for-loops yesterday... Is there anyway I can do it without advanced c++
Parsing strings is perhaps a bit too hard for a neophyte.
Dealing correctly with the vagaries of the floating point representation of numbers is also hard.

For now, accept the input as four numbers (integers), and attempt to find an integer root for the equation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int main()
{
    std::cout << "equation solver\n"
                 "looks for integer roots for 'ax + b == cx + d'\n"
                 "where a, b, c, and d are integers\n" ;

    int a ;
    std::cout << "enter an integer for the value of 'a' in 'ax + b == cx + d': " ;
    std::cin >> a ;

    int b ;
    std::cout << "enter an integer for the value of 'b' in '" << a << "x + b == cx + d': " ;
    std::cin >> b ;

    int c ;
    std::cout << "enter an integer for the value of 'c' in '" << a << "x + " << b << " == cx + d': " ;
    std::cin >> c ;

    int d ;
    std::cout << "enter an integer for the value of 'd' in '" << a << "x + " << b << " == " << c << "x + d': " ;
    std::cin >> d ;

    const int minv = -1000000 ;
    const int maxv = -1000000 ;
    std::cout << "attempting to find the integer root in [" << minv << ',' << maxv << "] of the equation\n    '"
              << a << "x + " << b << " == " << c << "x + " << d << "'\n" ;

    // TODO: check for a root n and print the results
    // for( int n = minv ; n <= maxv ; ++n )
    // {
        // TODO
        // ...
    // }
}
Topic archived. No new replies allowed.