Hi,

Im trying to compare an inputted string to an array of string. If the inputted matches one of the array of string it should return 0, but if not it should return 1. Any suggestion on how to do this?

This is my program:

{

for ( int j=0; j<9; j++)
{
if ((Color[0] == (ColorA[j])) == true)
return 0;
else 
return 1;
} 
}

Hi,

Im trying to compare an inputted string to an array of string. If the inputted matches one of the array of string it should return 0, but if not it should return 1. Any suggestion on how to do this?

This is my program:

{

for ( int j=0; j<9; j++)
{
if ((Color[0] == (ColorA[j])) == true)
return 0;
else 
return 1;
} 
}

To compare a string and an array of strings, the array should be accessed like Color_Array[j], but your inputted string should just be One_Color, with no brackets. Using One_Color[j] would access the J'th letter of that string, so that won't work.

As for your FOR loop, you can't have the "else" portion inside the loop. Because when the "IF" statement evaluates to false, it'll automatically go to the ELSE portion, and return 1 on the first go-through of the loop.

Thus you need:

for (int j = 0; j < 9; ++j){
  if (something == something) return 0;
}
return 1;  //so now it will return 1 if it goes through the entire 9 iterations and doesn't find a match

You should be using std::vector and std::string. Thus assuming that, you can do the following :

bool found = std::find( vectorOfStrings.begin(),vectorOfString.end(),stringToFind) != vectorOfString.end();

i choose to make it a "color[0]" because it is also an array but this type of array has a different category of comparison.

thank you for pointing that out.

i have a question not relating to the topic.
if i declare a 2 string numbers, how can i declare the combination to an integer?
for example:

string i = "1"
string j = "2"

string comb = i+j'

comb = "12"
can i declare "12" as an integer so I can use it for my formula?

i choose to make it a "color[0]" because it is also an array but this type of array has a different category of comparison.

thank you for pointing that out.

i have a question not relating to the topic.
if i declare a 2 string numbers, how can i declare the combination to an integer?
for example:

string i = "1"
string j = "2"

string comb = i+j'

comb = "12"
can i declare "12" as an integer so I can use it for my formula?

Two ways come to mind.

//1)
string thestring = "12";
int value = atoi(thestring.c_str());


//2)
#include <sstream>
.
.
.

stringstream ss;
string thestring = "12";
int value;
ss << thestring
ss >> value;

Wow thanks. i chose the first one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.