how to interpret string characters for if function (unit converter).

Hi, I'm really quite new to C++ but I'm trying to code a unit converter for a class assignment.

I'm stuck on trying to read an empty space before a character within a string.

For example, if I'm trying to convert from 'km' to interpret the k part, I use the following;
1
2
3
4
5
6
7
8
9
string UnitFROM;
double y,CC;

if (UnitFROM.at(found-1)=='k')
		{
			cout << "found kilo metre"<<endl;
		
			CC = y * 1e3;
		}


So in the above I can interpret 'km' and do something when this occurs, it works successfully, however when the initial unit is 'm' and I try a similar method, where I search for ==' ', it doesn't work. I've tried =='NULL' , ==NULL, ==empty() all with no avail.

I am at my wits end! I have a back up option where I measure the string length, and when it equals 1, and the one character within the string is == m I carry out a function, but this doesn't work either;
1
2
3
4
5
6
7
	if (UnitFROM.at(found) == 'm' && UnitFROM.length() == 1)
	{
		cout << "found metre"<<endl;
		//if (int(found)>0)
		
		CC = y * 1;
	}


Any suggestions or pointers would be greatly appreciated.

thanks!
Last edited on
== ' ' should work fine...are you sure there is actually a space there in your string?

Btw, use [code][/code] tags.
Apologies, tags have now been added.

There isn't a space in the string, the string will only consist of 'm' and when m is alone with no prefix, I want the code to recognise this and carry out a function, that's when I tried 'NULL.'

I guess overall I want to be able to detect the unique situation when a string has one character only and it is 'm,'
Topic archived. No new replies allowed.