Ajax
ASP
C,C++
C#,.NET
CSS,HTML
Delphi,Kylix
Experience
Flash
Java
JavaScript
PHP
Python
QT
SQL
ShellScript
VB
U :
P :
Register
Lost Password
Other Links
Code Archive
Save Giovanni
C,C++ Category Code
Real Name
:
Ahmet Tuna
Subject
:
Integer Test
Description
:
Sometimes we need a way to test the user input to know if it is an integer, i have
seen many algorythm for doing this,but a lot of them are incomplete since they only check
to see if the input contain the numeric values from 0 to 9 if so it is said that the input
is a number.Here you will fine a more precise way to test the user input for knowing if it
is an integer.
written by Gonzales Cenelia.
#include <stdio.h> #include <string.h> #include <ctype.h> // This is the function that makes the integer test bool IsInteger( char *number ) { int len = strlen( number ); bool isnumber = true; int i = 0; if( len == 0 || len > 9 ) // if the user did not enter an input { // or if the input is too big to be return false; // a "long" value. } while( i < len && isnumber ) // scanning the the input { if( isdigit( number[i] ) == 0 ) // check if there is a none digit character in the input { if( number[i] == '+' || number[i] == '-' ) // if so we verify if it is "+" or "-" { if( i + 1 > len - 1 || i - 1 >= 0 ) // check the position of "+" or "-" { // this signs could only be infront of isnumber = false; // the number,no where else. } } if( number[i] != '+' && number[i] != '-' ) // if it's not "+" or "-" than { // the expression is not a number isnumber = false; } } i++; } return isnumber; } void main() { char number[20]; // buffer for storing number printf("Enter a number: "); // geting a number from the user gets(number); if( IsInteger( number ) == false ) // testing the input number { printf("This is not an integer.\nOr maybe this expression is too big " "to be tested.\n"); } else { printf("Thanks for entering an integer !\n"); } // Since the number is stored as a string,if you need to convert // it to numeric value, you can use something like // "long numvalue = atol(number)" // you might start testing the program with the following numbers: // 14563,35.1223,-6853,+655 }
Rating
:
2.00
(out of 5)
Visitor Voting Booth
:
Excellent
Very Good
Good
Fair
Poor
Copyright © 2006 SharingCode.NET . All rights reserved. Hosted By: SisNetworks