How to Check if the VARIABLE is an INTEGER or not ?
i need help on solving a statement like this
thx in advanceCode:/*if (item == "%d") {
insert( &startPtr, item );
printList( startPtr );
}*/
Printable View
How to Check if the VARIABLE is an INTEGER or not ?
i need help on solving a statement like this
thx in advanceCode:/*if (item == "%d") {
insert( &startPtr, item );
printList( startPtr );
}*/
i think u should get its hexdecimal value .
less than 48 is an operator ,
between 49 - 58 is an integer
you meant by ASCII ?
hmm so there's no coding for it do so ??
i needed the if statement
You could also use isdigit(), found in ctype.h iirc.
Because C is type strict, if item is declared as an integer, then it's contents are an integer. You might want something like
item = strtol( string, NULL, 10 );
and then simply check to make sure that item is not zero. Google the function name to learn how to use strtol in a more bulletproof way.
I have check the description on wats a isdigit
<
The function isdigit() returns non-zero if its argument is a digit between 0 and 9. Otherwise, zero is returned. >
but i don't really understand wat it meant
isdigit simply returns true if it's argument (some character) is a digit, or false if it's not. I hope this makes sense.
Again, an integer will always have integer contents. If you're trying to convert some text (a string) to a usable number, there are functions for that. I can help you with that. If you're trying to do something else, then you need to be more specific about your problem, because I don't understand.
I think he means,
Of course, IsInteger() doesn't exist but it was put there for demonstrative purposes ;)Code:int myInt = 0;
char * myChar = NULL;
if(IsInteger(myInt))
/* is an int */
if(IsInteger(myChar))
{
}else {
/* isn't an int */
}
So, Do you want to know whether a variable was declared as an 'integer' ?
i tried doin so with the isdigit but it didn't work very wellCode:case 1:
printf( "Enter a integer: " );
scanf( "\n%d", &item );
if( isdigit(item) ) {
break;
}
else {
insert( &startPtr, item );
printList( startPtr );
break;
}
actually i'm trying to make a code
that prints a integer value
but if the user enter a char value i'm suppose to break or display a error message
tis actually my full coding , i'm having problem with case 1 ....Code:#include <stdio.h>
#include <stdlib.h>
struct listNode {
int data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
void insert( ListNodePtr *sPtr, int value );
int deleteNode( ListNodePtr *sPtr, int value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
int main()
{
ListNodePtr startPtr = NULL;
int choice;
int item;
instructions();
printf( "?" );
scanf( "%d", &choice );
while ( choice != 3 ) {
switch ( choice ) {
case 1:
printf( "Enter a integer: " );
scanf( "\n%d", &item );
insert( &startPtr, item );
printList( startPtr );
break;
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter integer to be deleted: " );
scanf( "\n%d", &item );
if ( deleteNode( &startPtr, item ) ) {
printf( "%d deleted.\n", item );
printList( startPtr );
}
else {
printf( "%d not found.\n\n", item );
}
}
else {
printf( "List is empty.\n\n" );
}
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}
printf( "?" );
scanf( "%d", &choice );
}
printf( "End of run.\n" );
return 0;
}
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}
void insert( ListNodePtr *sPtr, int value )
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = ( ListNode *)malloc( sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf( "%d not inserted. No memory available.\n", value );
}
}
int deleteNode( ListNodePtr *sPtr, int value )
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->nextPtr;
free( tempPtr );
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}
return '\0';
}
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
}
else {
printf( "The list is:\n" );
while ( currentPtr != NULL ) {
printf( "%d --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}
Well this is a linked list actually ......
well all this coding works perfectly if i type in a integer
but when i type in a char
it will keep looping like crazy
* Avoid using scanf(), use fgets() and sscanf(), See the FAQ.
* Don't cast malloc (see the FAQ)
Consider,
fgetc() for the choice... You won't need to check if it's an integer just don't do anything if it's not...
ASCII '1' != 1, just as ASCII '2' != 2.
you want:
Or convert choice to an integer first (don't cast it, convert it)Code:/* ... */
switch(fgetc(stdin))
{
case '1':
/* ... */
/* flush the stream now OR before you call fgetc(), See the FAQ */
this is my coding after i put isdigitCode:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct listNode {
int data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;
void insert( ListNodePtr *sPtr, int value );
int deleteNode( ListNodePtr *sPtr, int value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
int main()
{
ListNodePtr startPtr = NULL;
int choice;
int item;
instructions();
printf( "?" );
scanf( "%d", &choice );
while ( choice != 3 ) {
switch ( choice ) {
case 1:
printf( "Enter a integer: " );
scanf( "\n%d", &item );
if( isdigit(item) ) {
break;
}
else {
insert( &startPtr, item );
printList( startPtr );
break;
}
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter integer to be deleted: " );
scanf( "\n%d", &item );
if ( deleteNode( &startPtr, item ) ) {
printf( "%d deleted.\n", item );
printList( startPtr );
}
else {
printf( "%d not found.\n\n", item );
}
}
else {
printf( "List is empty.\n\n" );
}
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}
printf( "?" );
scanf( "%d", &choice );
}
printf( "End of run.\n" );
return 0;
}
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}
void insert( ListNodePtr *sPtr, int value )
{
ListNodePtr newPtr;
ListNodePtr previousPtr;
ListNodePtr currentPtr;
newPtr = ( ListNode *)malloc( sizeof( ListNode ) );
if ( newPtr != NULL ) {
newPtr->data = value;
newPtr->nextPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else {
printf( "%d not inserted. No memory available.\n", value );
}
}
int deleteNode( ListNodePtr *sPtr, int value )
{
ListNodePtr previousPtr;
ListNodePtr currentPtr;
ListNodePtr tempPtr;
if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->nextPtr;
free( tempPtr );
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;
while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}
return '\0';
}
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
}
else {
printf( "The list is:\n" );
while ( currentPtr != NULL ) {
printf( "%d --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}
i don't understand
This thread is getting terrible. :mad:
Much of what many C programs recieve as input will be text in the form of a string (aptly named a C string or character array). In order for the data to be interpreted flatly as anything besides text, you need to convert it to an integer. The standard library provides many functions that can do this for you. Options include: scanf, sscanf, strtoul, atoi, and others.
In order to use any of these functions, you need to understand their return values. The return values can be stored in a variable or checked in an if statement like any other variable can. After you know the conversion from text to integer was successful, you can rest assured that you did something right. But an int variable is always an integer, whether what is stored there is intentional or not.
Following this logic, scanf has two different outcomes in this example, depending on how many items were assigned:
It just so happens that printf also converts integers back to text for the terminal your program is running on.Code:int foo = 0;
if( scanf( "%d", &foo ) == 1 ) {
printf( "You probably typed %d\n", foo );
} else {
printf( "You should have typed an integer.\n" );
}
This topic, converting from integers to strings, and strings to integers, is covered very well in our FAQ.
Clear?
err ... having a headache ... :(
what should i do now .... ?