Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-18-2005, 06:23 PM   #1 (permalink)
lalabear
Registered User
 
Join Date: Jul 2005
Posts: 4
lalabear is on a distinguished road
how to do the is numeric ?

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
lalabear is offline   Reply With Quote
Old 07-18-2005, 11:19 PM   #2 (permalink)
webcomplete.com
Registered User
 
Join Date: Mar 2005
Posts: 14
webcomplete.com is on a distinguished road
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
webcomplete.com is offline   Reply With Quote
Old 07-19-2005, 06:51 AM   #3 (permalink)
Feis
Registered User
 
Join Date: Apr 2005
Posts: 18
Feis is on a distinguished road
minor quibble, but when checking whether the character is a digit you can use the isdigit() function from the ctype lib(you'd have to #include<ctype.h>). its a boolean function, so you could just change this:
if( !( (*l_Str)>='0' && (*l_Str)<='9') )

to this:
if( !(isdigit(*l_Str)))

The ctype lib has a bunch of functions for checking your input for alphanumeric, lowercase, numeri, alpha, etc. so its pretty useful
Feis is offline   Reply With Quote
Old 07-19-2005, 06:52 AM   #4 (permalink)
webcomplete.com
Registered User
 
Join Date: Mar 2005
Posts: 14
webcomplete.com is on a distinguished road
Quote:
Originally Posted by Feis
minor quibble, but when checking whether the character is a digit you can use the isdigit() function from the ctype lib(you'd have to #include<ctype.h>). its a boolean function, so you could just change this:
if( !( (*l_Str)>='0' && (*l_Str)<='9') )

to this:
if( !(isdigit(*l_Str)))

The ctype lib has a bunch of functions for checking your input for alphanumeric, lowercase, numeri, alpha, etc. so its pretty useful
no arguments.
webcomplete.com is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



All times are GMT -8. The time now is 06:45 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting