Quote:
|
Originally Posted by lalabear
how do u do the if statement for numeric lie:
string abc;
if(abc = numeric)
cout << "abc is numeric";
pls giv me the code thx pls also inclue all the stuff like #include<iostream> coz is scared i confuse thx u greatly
|
Here is the answer to your homework:
Code:
#include <iostream>
#include <string>
using namespace std ;
const bool IsNumeric(string a_Str)
{
// COPY a pointer to string
const char * l_Str = a_Str.c_str() ;
// ITERATE each character in the string
while( *l_Str )
{
// CHECK if current character is non-numeric
if( !( (*l_Str)>='0' && (*l_Str)<='9') )
// FOUND non-numeric
return false;
// NEXT char in string
l_Str ++ ;
}
// RETURN is numeric = true
return true ;
}
void main()
{
string l_Str ;
l_Str = "123" ;
if( IsNumeric(l_Str.c_str()) )
cout << l_Str << " is numeric." << endl ;
else
cout << l_Str << " is NOT numeric." << endl ;
l_Str = "one hundred twenty three" ;
if( IsNumeric(l_Str) )
cout << l_Str << " is numeric." << endl ;
else
cout << l_Str << " is NOT numeric." << endl ;
}
Of course this doesn't handle formatted numeric like : 1,200,000 or 100.00 - but its just homework, im sure the professor isnt looking for a million dollar code - did you say your majoring in computer science?
Glad I could corrupt you
