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?
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)
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:
#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>usingnamespace 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;
}