String woes

I've been designing a program that takes two user-entered words, removes the characters that are in both and then outputs the result. I'm not sure where exactly the problem lies in my code but it is most likely in my finding out if two characters are the same and in the removing of these characters. I really appreciate any help anyone can give me.
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
#include <iostream>
#include <string>

using namespace std;

int i = 0;
int j = 0;
int n = 0;
string r;
string q;

int main()
{
 
cout << "Enter a word" << "\n";
getline(cin, r);
cout << "Now enter another" << "\n";
getline(cin, q);                          
int l = r.length();
int m = q.length();

char c[l];   
char d[m];

for (n=1;n<150;n++){
if(i == l){
i=0;
j++;
}
if (c[j] == d[i]){
r.erase(i,1);
q.erase(j,1);
}

else {

}
i++;
}
cout << r+q << endl;
system("pause");
return 0;
}
This is an interesting solution where normally I would expect to see an embedded for loop. Your 'i' integer used to keep track of the character in the 'd' string array will only get promoted to the second place and then it will get knocked back down to 0 again so line 30 will only ever execute once and only when the first two characters of 'd' and 'c' match.

By the way, why are you comparing 'c' to 'd' when the user is entering 'r' and 'q'?

EDIT: Ok I'm writing up an embedded for loop with psuedo-code, using your variables, so that you see what you should be doing here.
Last edited on
You should be doing something closer to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
for(n = 0; n < r.length(); ++n)/*Use your origional strings instead of those char arrays*/
{/*NOTE: string.length() returns the number of letters but the array starts at zero*/

   for(j = 0; j < q.length(); ++j)/*Go through each letter in q and match it against the letter in r*/
   {/*NOTE: See the note above it also applies here*/
		if(r[n] == q[j])/*If the letters match*/
		{/*Do whatever*/}/*End of if(...)*/
		
		}/*End of for(j = 0; j < q.length(); ++j)*/
		
	}/*End of for(n = 0; n < r.length(); ++n)*/
	
	/*Continue your code*/


At line 1 in my code we make sure that n is set to zero, I know you did that when you initialized it but I did it anyway out of habbit. We then check n agains then length of the word stored in your string 'r', this makes it so that it doesn't really matter what is entered for 'r' we check n against the length of it no matter what. Then we increment n.

NOTES: The integer 'n' is our place holder for the 'r' string, which is a dynamic array of chars. We are not using your 'c' or 'd' arrays because the string class has all of the functions we need for your task built in already. Remember that 'n' will not increment until the end of 'for(...)' loop.

Line 2 is something to keep in mind when using this in the future. If the 'r' string is 5 letters long then 'r.length()' will return 5 but checking anything against the 5 position in an array that only goes up to 4 is wrong.

Line 4 resets the 'j' integer to zero, this time we want to do it because the second time it executes and on it will have a value stored in it. We then compare the integer 'j' against the length of the 'q' string for the same reasons we do it above with the 'n' int and the 'r' string. When the code with in the loop makes a pass we increment 'j'.

NOTE: Notice how 'n' does not increment until after 'j' has counted all the way up to q.length() this ensures that we are checking each letter in r against each letter in 'q'. This is why it is important to reset 'j'.

Line 6 is our comparision. The only thing to note here is how I am using 'q' and 'r' as arrays, this is because the class 'string' is actually an array of chars with some cool additional stuff built on.
Last edited on
Thanks for the help, I think I understand everything you're saying. Problem is the program doesn't run properly. Here's the code I have now:
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
#include <iostream>
#include <string>

using namespace std;

int i = 0;
int j = 0;
int n = 0;
string r;
string q;

int main()
{
 
cout << "Enter a word" << "\n";
getline(cin, r);
cout << "Now enter another" << "\n";
getline(cin, q);                          
int l = r.length();
int m = q.length();

for(n = 0; n < l; ++n)
{

   for(j = 0; j < m; ++j)
   {
		if(r[n] == q[j])
		{
        r.erase(n,1);
		q.erase(j,1);
        }
		
		}
		
	}
cout << r+q << endl;
system("pause");
return 0;
}


I think I am probably wrong with the erase part but it's all I could come up with. I keep getting a runtime error. Thanks again
So i have this at the moment, and it's almost working but it seems to be ignoring the first character of the second string. If you enter "computer" and "program", for example, it returns "cpute" instead of "cute".

Anyone have any ideas?

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>

using namespace std;

int i;
int j;
string x;
string y;

int main()
{   
            
        
        cout << "\n Enter a word \n";
        getline (cin, x);
        
        cout << "\n Enter another word \n";
        getline (cin, y);
        
        int z = x.length();
        int w = y.length();
        
    
        for (i = 0; i < z; ++i)
        {
            for (j = 0; j < w; ++j)
            {
                if(x[i] == y[j])
                { 
                   x.erase (i,1); 
                }
            }
        }
   
        cout << "Result:" << x << "\n";
     
        system ("pause");
        return 0;
}   
Topic archived. No new replies allowed.