473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Working with strings

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

May 29 '06 #1
3 2658

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

May 29 '06 #2
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
May 29 '06 #3
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

May 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adam T. Gautier | last post by:
I have been unable to solve a problem. I am working with MD5 signatures trying to put these in a database. The MD5 signatures are not generated using the python md5 module but an external...
2
by: Shaun | last post by:
Hi Odd one here, I have a local version of SQL running on my laptop for demos, been working fine for years yet yesterday I suddenly get an error "Invalid Conection Parameter" running a sproc...
0
by: Robert Misiak | last post by:
Hi all- I'm trying to add Windows 98/ME support to an app that previously only supported Windows 2000+. My test box is a running 98 SE. I have a number of localized forms (translated for...
2
by: Noozer | last post by:
Hi! Trying to get my head around working with network connectivity in VB.Net. I've gotten a fairly decent control based on the sample in the 101VBSamples available from Microsoft.What I'm having...
2
by: John | last post by:
I am trying to call a third party company's dll that I have no control over. They tell me it was written in C. The declaration in VB6 is as follows: Private Declare Sub csub Lib "rlpolk.dll"...
3
by: R Reyes | last post by:
Error: Unable to cast object of type 'ASP.CompanyDirectory_aspx' to type 'CompanyDirectory'. Code: clsCompanyDirectory = (CompanyDirectory)Context.Handler;...
6
by: Ptaku25 | last post by:
Hi all // my code var string1= "Pablo has 3 cats and 1 dog"; var string2= "Pablo has 3 cats"; var str1 = ""; var str2 = ""; str1 = string1.match(string1, "g");
1
by: suresh | last post by:
Hello all, I have a problem while working with fixed length stings in Vb.net The problem is, when I call a Function FormInfo to return a FormName, which is a string value, it is returning some...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
3
by: Jon Slaughter | last post by:
mcrypt_enc_get_iv_size is not randomizing the vector. I have tried to randomize the seed in various ways but it always returns the same output. My current code as follows: for($i = 1; $i < 5;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.