Comparing chars -.-

Hi

I know this is the simplest thing ever but everything I tried failed surprisingly :/

Basically i want an input that is "blue car". And I want a condition to be true when its both "blue car" , "bluecar" , "Blue car" and "Blue Car".

That space character is really bugging me.
I'm pretty sure its possible but i cant find a way :/


Thanks for your time
Use the || operator? As in:
 
if (input == "blue car" || input == "bluecar" || input == "Blue car" || input == "Blue car")
Yea I tried that but it only works when the input is bluecar.. I feel like I am doing something wrong could you please check this?



#include <iostream>
#include <cstdio>

using namespace std;

main(){
string input;
cin >> input;

if (input == "blue car" || input == "bluecar" || input == "Blue car" || input == "Blue car")
{
cout << "Success\n";
}
else
{
cout << "Fail\n";
}
system ("pause");

}
Well you could write a simple function that removes all spaces and forces every character to a lower case. I have gone ahead an written the function for you, feel free to use it. This is a pretty sloppy way of going about this but I only spent a few minutes on it so feel free to make whatever changes you like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void SimplifyString( std::string &Original )
{
	int Length = Original.length();

	for ( int i = 0; i < Length; i ++ )
	{
		if ( isspace( Original.at( i ) ) )
		{
			Original.erase( i, 1 );
			i = 0;
			Length = Original.length();
		}

		Original.at( i ) = tolower( Original.at( i ) );
	}
}

void main()
{
	std::string Test = "TEST     STRring 1233\t\tblah";
	SimplifyString( Test );
	std::cout << Test; // Output: teststring1233blah
}
Last edited on
On my compiler, VC++ express 10, only "blue" gets stored in input when the input includes a space.

Might want to use getline and assign the input to a string.
Last edited on
Wow that works codingshark thank you very much, kudos.

Topic archived. No new replies allowed.