comparing strings

So i implemented this particular function to compare strings: http://pastebin.com/hxFFfaTZ

but i keep getting this error http://i.imgur.com/sxUZ4Hj.png

Any ideas?
As someone told me before, you can't compare strings with '=='. You have to use strcmp (part of <cstring>) or compare (part of <string>... maybe that'S the issue... also, don't forget to use

1
2
cin.sync();
cin.clear();


before using getline(), as something could remain in the stream buffer of cin or something like that, mostly if you've used cin >> before in the same code.
Try this:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>

using namespace std;

const string days [7] =
{
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday"
};

bool is_valid (string dayInput);


int main()

{
    string day;

    cout << "Enter a day of the week:  ";

    cin.sync();
    cin.clear();

    getline(cin, day);

    if (!(is_valid(day)))
        cout << "You got it wrong!\n";
    else
        cout << "You got it right!\n";




    return 0;
}

bool is_valid (string dayInput)
{
    int ret = 0;

        for (int i = 0; i < 7; ++i)
        {
            if (dayInput.compare(days[i]) == 0)
            {
                ret = 1;
                break;
            }
        }

    return ret;

}
or...

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>

using namespace std;

const char* days[7] =
{
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday"
};

bool is_valid (string dayInput);


int main()

{
    string day;

    cout << "Enter a day of the week:  ";

    cin.sync();
    cin.clear();

    getline(cin, day);

    if (!(is_valid(day)))
        cout << "You got it wrong!\n";
    else
        cout << "You got it right!\n";




    return 0;
}

bool is_valid (string dayInput)
{
    int ret = 0;

        for (int i = 0; i < 7; ++i)
        {
            if (dayInput.compare(days[i]) == 0)
            {
                ret = 1;
                break;
            }
        }

    return ret;

}


Only line 6 has changed.
I see. Thanks for the help!
Last edited on
As someone told me before, you can't compare strings with '=='.


You can't compare a C-style string with a C-style string using == (doing so compares the address of one C-style string to another.) You can however compare a std::string with a std::string or a std::string with a C style string using ==

The problem with the OP's code is that he had a two dimensional array of strings, when he needed a one dimensional array of strings, and that he was accessing outside of the bounds of that array.


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
// ...

// const string days[7][10] =
const string days[7] =
{
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday"
};

// ...

bool is_valid (string dayInput)
{
    int ret = 0;
 
    for (int i = 0; i < 7; ++i)
    {
        // if (dayInput == days[i][10])
        if (dayInput == days[i])
        {
            ret = 1;
            break;
        }
    }

    return ret;
}

Last edited on
my code works too! :-p

but you're right about the array I spoted that...
Last edited on
Topic archived. No new replies allowed.