some C++ string stuff
i need to do a number of things with strings, for my game console. i have a parser for a char * right now, it goes from the beginning of the string, and adds each character to a char * var. when it finds a space or an ''='' it finds the next non-space or non= character, and puts the rest in a char * value. now, i have no trouble with the var part, and i have no problem with the value part except for those that are numbers. like if the user typed in gamespeed = 0.01, it wouldn''t know what the number was, since it was treating it like a string. is there a way to get a string that equals "0.01" or any number for that matter into that number? also, is there a way to make all the lowercase characters in a string uppercase? just makes sure that the user can input any command in any case and i can just compare it to an uppercase version. thanks
You can use sscanf() to search for numbers in a string.
Like this:
char String[] = "0.01";
float Value;
sscanf(String, "%f", &Value);
Cheers!
Like this:
char String[] = "0.01";
float Value;
sscanf(String, "%f", &Value);
Cheers!
Indicates that a floating point value (&value, seen at the end) should be placed there in place of the %f
eh?
eh?
eh?
ok cool now the only thing i need is a way to make all of a string''s lowercase characters uppercase. so the entire string is uppercase, that way i can have people input in any case and have it not care. i guess i could get the poitner to the string, and if the value is in the lowercase characters'' range, add the difference bewtween lowercase and uppercase characters, but it would be nice if there was just a command to do that
Billy - use the islower() and toupper() functions contained in ctype.h header file.
You could do
if (islower(char))
char=toupper(char);
That should sort you out.
You could do
if (islower(char))
char=toupper(char);
That should sort you out.
quote: Original post by SteveBe
Billy - use the islower() and toupper() functions contained in ctype.h header file.
You could do
if (islower(char))
char=toupper(char);
That should sort you out.
ok cool, that looks good. since i read the input strings on a per character basis its super easy to just put this in the loop with the rest of the stuff
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement