Linked List if statement using a string variable

I am attempting to use the data off a link list node in an if statement the data is in string format.
The string data is always either "yes" or "no". If its a yes I want to add to an int that is called survivors. I have it looping the list fine and it outputs the data from the node fine. However it won't add to the survivors. Is there something special I have to do for an if statement that uses a string variable off a link node?

code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int PassengerList::countSurvivors()
{
        int survivors = 0;
        Passenger *temp_passenger = root;
        while ( temp_passenger != NULL)
        {
                if(temp_passenger->survival== "yes")
                {
                        survivors = survivors ++;
                }
        cout << temp_passenger->survival<<endl;
        temp_passenger = temp_passenger->next;

        }
        cout << " The number of survivors total is : " << survivors << endl;
}
Last edited on
Does no one know how to help?
First, we can't see Passenger's declaration, so we're guessing.

Second, surround code with with code tags (hit the <> button on the right to see).

If survival is a std::string, then your check will work if the content is lower case.

If the content is mixed case or upper case, it won't work. You have to convert the string to lower case before the test.

Further, in any case, the content must be just those three letters, no spaces, no punctuation...exact match.

You can convert to lower case using the stl utility std::transform, with ::toupper.

1
2
3
4
5
6
std::string s = "UPPER or Mixed case";

std::transform( s.begin(), s.end(), s.begin(), ::toupper );

std::cout << s;


The output would be the lower case version of the string.

If survival is a character array, you'd have to use strcmp, or the safer strncmp, to compare strings.
Last edited on
I am reading from a document, so there are most likely spaces within the string. Is there a way to calculate for that? Otherwise I will need to come up with some other way of comparing. Otherwise yes all of the string are for sure all lower case. Also yes it is a string.
Last edited on
Just got back in....ok, look for std::string::find, searching within a string for "yes", anywhere within the string.

Topic archived. No new replies allowed.