How to compare two string arrays

Hello,
I am writing a program in C++(using MFC for GUI) that takes in a string input from user. After reading user input, I want to check to see if it contains any letters in a string array that I have defined.If the input matches a letter in the string array I do some action. The question, I have is how to compare two string arrays, I keep getting errors I have done the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CString charlist [2] [6]//string array
{
  {"A","B","C","D","X","F"}
  {"8","9","10","11","12"}
};
CString input {"FX333")//hard coded for post but will be entered by user via GUI
//sscanf(input.GetBuffer(5), "%s") -is used to retrieve user's input, input is also of type CString.
CString inputholder = "";
for (int i = 0. i <5; i++)
{
 if(input[i] == charlist[i,i])// keep getting error for this line
{
   if(inputholder == "")
  {
    inputholder = charlist[i+1,i];
  }
}
  else
  {
    inputholder = inputholder + charlist[i+1,i];
  }
}


I will really appreciate any help!!!!!
Thanks In advance!!
Right off the bat:
You have a period after int i = 0 in your for loop.
When i gets to 2, you will be out of bounds on the first element of charlist (because it's defined as 2 and valid offsets will be 0 and 1.)
Also charlist[i][i] is a CString while input[i] is a char.
Last edited on
Thanks,

Kooth,
Sorry the period in the for loop was a mistake, I don't have that in my program. I have mofified code and am still having program. I changed my if statement to:
1
2
3

 if(input[i] == charlist[0,i]) //error states '==' no conversion from 'class CString *' to 'nt'


I don't see a problem with this.

Caligulaminus,
I am not understanding, both charlist and input are defined as CString.
What Caligulaminus is saying is that to look at a specific CString in charlist, you use the syntax charlist[ 0, i ], which is what you have, but not correct for what you are trying to do. To look at a character of that CString, you can do this:

1
2
3
4
5

CString temp = charlist[ 0, i ];

if( input[ i ] == temp[ i ] )


Kooth,
Thanks. I have included what you suggested and am still getting errors.

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
CString charlist [2] [6]//string array
{
{"A","B","C","D","X","F"}
{"8","9","10","11","12"}
};
CString input {"FX333")//hard coded for post but will be entered by user via GUI
//sscanf(input.GetBuffer(5), "%s") -is used to retrieve user's input, input is also of type CString.
CString inputholder = "";
for (int i = 0, i <5; i++)
{
CString temp = charlist[ 0, i ];//error states initializing : cannot convert from class CString [6] to class CString


if(input[i] == temp[i])
{
if(inputholder == "")
{
inputholder = temp;
}

else
{
inputholder = inputholder +temp;
}
}
else
{
if(inputholder == "")
{
inputholder = input[i];
}
else
{ 
inputholder = inputholder + input[i];
}
}
}




I don't see a problem with this.

I will really appreciate any help!!!!!
Thanks In advance!!
Maybe the ampersand is missing?:

1
2
3
4

CString temp = &charlist[ 0, i ];

Kooth,
I am still getting an error. Do you think it has something to do with charlist[] being a 2-d array and temp is just a string(well a 1-d array). Do you think this is a MFC problem?
I don't have a clue on why this isn't working!!!!! It seems to be straight forward.
What error do you get with the ampersand?
An error I overlooked before: it is charlist[0][i] not charlist[0,i].
Kooth,
I am getting the same error as before -initializing : cannot convert from class CString [6] to class CString

Caligulaminus is right: Take out the ampersand and use:

1
2
3

CString temp = charlist[ 0 ][ i ];


Also, your charlist should look like this:

1
2
3
4
5
6
7
8

CString charlist [2] [6]  = 
{
{"A","B","C","D","X","F"},
{"8","9","10","11","12"}
};

Ok, it is working now!! Thanks soo much. I have a question though, way did I have to code my code to:
1
2
3

CString temp = charlist[ 0, i ];


I thought this would work as well:
1
2
3

input[i] == charlist[0,i] 




Thanks again guys!!!
Again, charlist is a two-dimentional array of CStrings. The syntax you are using shouldn't even compile: It should be charlist[ 0 ][ i ]. This gets you the CString on row zero, column i. Then you still need to loop through every character of each CString. You really need a second for loop for that!

Topic archived. No new replies allowed.