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:
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.)
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 ] )
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!!
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.
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!