How to verify if a string equals a certain word

Hi there,

I want to see if the value of a string equals a certain text. But I can't make it. There are two problems :
1) It seems 'tolower' doesn't work with strings. What to use then?
2) When I delete tolower, it works, but I have an output of "You didn't enter 'add'" even when I do enter 'add'. What's the problem? Is it due to the null byte? What to change?

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
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[]) //This was added automatically by Visual Studio. No idea what it does.
{
	return 0;
}

int main()
{

	char input[64];

	cout<<"Please Enter 'add'"<<endl;
	cin>>input;
	if (tolower(input)=="add")
		 cout<<"You entered 'add'" <<endl;
	else cout<<"You didn't enter 'add'"<<endl;

	return 0;
}
If you actually used strings, comparison would work:
(also, you can drop the stuff that visual studio adds, although if you drop stdafx.h, you need to untick 'use precompiled headers' in project settings)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string input;

    cout << "Please Enter 'add'\n";
    cin >> input;
    if(input == "add")
        cout << "You entered 'add'\n";
    else
        cout << "You didn't enter 'add'\n";
}


tolower for strings exists in boost, but in core C++ you'd have to do it yourself. There are several ways, the typical textbook approach is to use foreach or transform:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>
using namespace std;

void lower(char& c)
{
    c = tolower(c);
}

int main()
{
    string input;

    cout << "Please Enter 'add'\n";
    cin >> input;
    for_each(input.begin(), input.end(), lower);
    if(input == "add")
        cout << "You entered 'add'\n";
    else
        cout << "You didn't enter 'add'\n";
}


boost solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace std;

int main()
{
    string input;

    cout << "Please Enter 'add'\n";
    cin >> input;
    boost::to_lower(input);
    if(input == "add")
        cout << "You entered 'add'\n";
    else
        cout << "You didn't enter 'add'\n";
}

online demo: http://liveworkspace.org/code/3LmKYa
Last edited on
EDIT: Cubbi beat me, except he forced you to use boost.

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
#include "stdafx.h" //create a new "Blank" project and paste the new code in
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int _tmain(int argc, _TCHAR* argv[]) //This was added automatically by Visual Studio. No idea what it does.
{
	return 0;
}

int main()
{
	char input[64];
	string input;
	cout<<"Please Enter 'add'"<<endl;
	cin>>input;
	if (tolower(input)=="add")
	transform(input.begin(), input.end(), input.begin(), ::tolower);
	if(input == "add")
	{
		cout<<"You entered 'add'" <<endl;
	}
	else cout<<"You didn't enter 'add'"<<endl;

	return 0;
}


Working demo: http://ideone.com/Z9xKG4
Last edited on
Thank you both! This helps a lot.
Topic archived. No new replies allowed.