## enkidu75

I am making a program for my class where I handle equation problems such as:
2a-3b+5c=10
3a-2b-3c=-5 where the answer would be:
5a-5b+2c=5

This is what I have so far. It still isn't perfect cause I still haven't figured out the "=" part. I do have it performing the math though. But the answers are coming as:
5a5b5c-5a5b5c+2a2b2c. I don't understand why. Any help would be much appreciated.

``````#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

// Declarations
const int length=26;
int num_var,
i,j;
char variable_array [length];
int equ_1 [length],
equ_2 [length];

int main()
{
cout<< "How many variables are in your equation?" <<endl;
cin>> num_var;

cout<< "Enter the variables for the first equation," <<endl;
cout<< "while entering a negative sign if the preceding" <<endl;
cout<< "operator is a minus sign." <<endl;
for (i=0; i<26 && i<num_var; i++)
{
cin>> equ_1[i];
}

cout<< "Enter the variables for the second equation," <<endl;
cout<< "while entering a negative sign if the preceding" <<endl;
cout<< "operator is a minus sign." <<endl;
for (i=0; i<26 && i<num_var; i++)
{
cin>> equ_2[i];
}

strcpy (variable_array, "abcdefghijklmnopqrstuvwxyz");

for (i=0; i<26 && i<num_var; i++)
{
equ_1[i] = equ_1[i] + equ_2[i];
}

for (i=0; i<26 && i<num_var; i++)
{
for (j=0; j<26 && j<num_var; j++)
{
cout<< equ_1[i] << variable_array[j];
}
}
}
``````

## thelamb 163

You first loop through i, and inside this for loop you loop through j.
So the sequence will look like:

i j output
0 0 "5a"
0 1 "5b"
0 2 "5c"
1 0 "5a"
1 1 "5b"
1 2 "5c"
2 0 "2a"
2 1 "2b"
2 2 "2c"

(I excluded the "-" and "+")

So... now you need to think of a way to fix that ;)

## enkidu75

Thanks for helping. Can you tell me this? I got it working with a while loop instead. But it has the same arguments as my original for loop did. Why does it work this way?

``````cout<< "Your answer is: ";
i=0;
j=0;
while (i<length && i<num_var)
{
cout<< equ_1[i] << variable_array[j];
i++;
j++;
}
``````

## thelamb 163

Now you are only looping once, and incrementing _both_ i and j _at the same time_.
Are you sure that you understand why your first solution, with 2 for loops, does not work?

## enkidu75

Not really. Is it because I used to for loops? Would it have worked liked this while loop if I had just used one for loop?