Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Working with strings

Reply
Thread Tools

Working with strings

 
 
madhu
Guest
Posts: n/a
 
      05-29-2006
i have a string " 1; 200; 03; 4567; a; b; 7; 11; 9; 0.01; 0.11; 0,12;
140; 15; 16; 17;"

i want to check for alphabetical values from a to z .when i find them i
should give a mesage
incorrect input
std::vector<float> StringMap::getValueMatrix(const std::string& key)
{
std::string value = getValueString (key);
vector<float> matrix_value;
stringstream msg;

while(!value.empty()) {
string::size_type pos1 = value.find(";");
string::size_type pos2 = value.find(":");
//checks for ;, : in value
if(pos1<pos2)
{

if (!isdigit(value.substr(0, pos1).c_str()))
{
cout<<"Not a valid input";
break;
}
matrix_value.push_back (atof(value.substr(0, pos1).c_str()));
value.erase(0, pos1 + 1);
}
else
{
if (isdigit(value.substr(0, pos2).c_str()))
{
cout<<"Not a valid input";
break;
}
matrix_value.push_back (atof(value.substr(0, pos2).c_str()));
value.erase(0, pos2 + 1);
}
}

return matrix_value;
}

i tried using isdigit()
but i get an error
c:\_users\mbanda\veo_win32_workspace\tools\src\Int eractionServer\VeoKit\StringMap.cxx(339):
error C2664: 'isdigit' : cannot convert parameter 1 from 'const char *'
to 'int'

please can anyone help me to know the correct method to do it

thanks in advance

madhu

 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      05-29-2006

madhu wrote:

[...]

> if (!isdigit(value.substr(0, pos1).c_str()))


[...]

> please can anyone help me to know the correct method to do it


AFAIK its something like so :

if( (pos1 != std::string::npos) // npos if not found
&& ( ! isdigit(value.at(pos1) ) ) ){
{
std::cout<<"Not a valid input\n";
}
}
// or
if( (pos1 != std::string::npos)
// requires #include <locale>
&& ( ! isdigit( value.at(pos1),std::locale::classic() ) ) ){
{
std::cout<<"Not a valid input\n";
}
}

regards
Andy Little

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-29-2006
madhu wrote:
> i have a string " 1; 200; 03; 4567; a; b; 7; 11; 9; 0.01; 0.11; 0,12;
> 140; 15; 16; 17;"
>
> i want to check for alphabetical values from a to z .when i find them
> i should give a mesage
> incorrect input


The most common way of doing that is to convert all the values as you
normally would (using a string stream or 'strtod', for example, and if
a conversion fails, report the error.

> std::vector<float> StringMap::getValueMatrix(const std::string& key)
> {
> std::string value = getValueString (key);
> vector<float> matrix_value;
> stringstream msg;
>
> while(!value.empty()) {
> string::size_type pos1 = value.find(";");
> string::size_type pos2 = value.find(":");
> //checks for ;, : in value
> if(pos1<pos2)
> {
>
> if (!isdigit(value.substr(0, pos1).c_str()))


OK, RTFM on 'isdigit' and look up on the web how it's to be used.

Briefly: it checks a _single_character_, not the whole string. By
definition, if your 'subst' is, say, "4567", is it *a* digit? No.
It's a bunch of them. How can you check a string for being a digit?
You can only check one symbol. You could rewrite your program to
check _each_ character of that substring, or you could simply convert
it as you normally would, but using a _proper_ function, like 'strtod'
and if the pointer to the end of the converted sequence (again, RTFM)
is not returned as the end of the string, there is an invalid symbol
somewhere in the string you're trying to convert.

> {
> cout<<"Not a valid input";
> break;
> }
> matrix_value.push_back (atof(value.substr(0, pos1).c_str()));


Do NOT use 'atof' anywhere in your production code. It's a very bad
function. Its existence ought to have been cancelled in C++.

> value.erase(0, pos1 + 1);
> }
> else
> {
> if (isdigit(value.substr(0, pos2).c_str()))


Same sentiment as above. Read about 'isgidit' in you C book.

> {
> cout<<"Not a valid input";
> break;
> }
> matrix_value.push_back (atof(value.substr(0, pos2).c_str()));
> value.erase(0, pos2 + 1);
> }
> }
>
> return matrix_value;
> }
>
> i tried using isdigit()
> but i get an error
> c:\_users\mbanda\veo_win32_workspace\tools\src\Int eractionServer\VeoKit\StringMap.cxx(339):
> error C2664: 'isdigit' : cannot convert parameter 1 from 'const char
> *' to 'int'
>
> please can anyone help me to know the correct method to do it


There is usually more than one correct method to do it. You will have
to choose one and use it.

Conversion from a stream into a set of number is a very common problem
and you should be able to find several solutions in every decent C++
book. What book are you reading that doesn't have a chapter on stream
I/O with error detection?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
madhu
Guest
Posts: n/a
 
      05-30-2006
thanks for your i mail .i have changed total implementation of my
function.here is my code

std::vector<float> StringMap::getValueMatrix(const std::string& key)
{
vector<float> defRetValue ;
std::string value = getValueString (key);
vector<float> matrix_value;

while (!value.empty ())
{
//checks for ;, : in value
string::size_type pos = value.find_first_of (";:");
if (pos == string::npos)
{
//Return default return value
cout << "end of the value";
//return(defRetValue);
break;
}

string keyValue = value.substr (0, pos);
bool letter = false;
for (string::size_type i = 0; i < keyValue.size (); i++)
{
letter = letter || (isalpha (keyValue[i]) != 0);
}
if (!letter)
{
matrix_value.push_back (atof (keyValue.c_str ()));
}
value.erase (0, pos + 1);
}

cout << "Debug: Within getValueMatix" << endl;
for (vector<float>::size_type i = 0; i < matrix_value.size (); ++i)
{
cout << matrix_value[i] << " ";
}
cout << endl;

return matrix_value;
}


Thanks for your suggestions.I was actualy confused with some functions
..Now am clear with the concept

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57