473,408 Members | 1,700 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,408 software developers and data experts.

Variable check

11
Below is my coding , how do i check whether the user input is
interger or not ...... because if the user entered a integer i would like to ask
the user to re-enter ...... meaning this program will only accepts char
thanks in advance





#include <stdio.h>
#include <stdlib.h>

struct stackNode {
int data;
struct stackNode *nextPtr;
};

typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;

void push( StackNodePtr *topPtr, int info );
char pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );
void instructions( void );
char Full();

int main()
{
StackNodePtr stackPtr = NULL;
int choice;
char value;
int timer;
timer = 0;
int timer2;
timer2 = 0;
timer = Full();
timer2 = timer;
instructions();
printf( "?" );
scanf( "%d", &choice );

while ( choice != 3 ) {

switch ( choice ) {

case 1:
timer2 = timer2 + 1;
if ( timer2 >= 11 )
{
printf("The stack is full\n");
printStack( stackPtr );
timer2 = timer2 - 1;
break;
}

else
{
printf( "Enter an character: " );
fflush(stdin);
scanf( "%c", &value );
push( &stackPtr, value );
printStack( stackPtr );
break;
}
}

case 2:

if ( !isEmpty( stackPtr ) ) {
printf( "The popped value is %c.\n", pop( &stackPtr ) );
}
printStack( stackPtr );
timer2 = timer2 - 1;
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 choice:\n"
"1 to push a value on the stack\n"
"2 to pop a value off the stack\n"
"3 to end program\n" );
}
void push( StackNodePtr *topPtr, int info )
{
StackNodePtr newPtr;

newPtr = ( StackNode * )malloc( sizeof( StackNode ) );

if ( newPtr != NULL ) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
}
else {
printf( "%c not inserted. No memory available.\n", info );
}
}

char pop( StackNodePtr *topPtr )
{
StackNodePtr tempPtr;
char popValue;
tempPtr = *topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr;
free( tempPtr );

return popValue;

}

void printStack( StackNodePtr currentPtr )
{
if ( currentPtr == NULL ) {
printf( "The stack is empty.\n\n" );
}
else {
printf( "The stack is:\n" );

while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}
printf( "NULL\n\n" );
}
}

int isEmpty( StackNodePtr topPtr )
{
return topPtr == NULL;
}

char Full()
{
int test;
test = 0;
return test;
}
Jul 13 '07 #1
2 2116
Hi
to check if the input is an integer, you could either check if the character entered is between '0' and '9' or check using isdigit function.
Jul 13 '07 #2
kky2k
34
as far ur requirements do follow "devicacs" suggested..
Coz' the only way to find the size of an integer in c is using sizeof("integer")..
That too compiler/O.S dependant..so it varies from machine to machine..
Jul 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Josiah Manson | last post by:
Hello. I am very new to Python, and have been unable to figure out how to check if a variable exists or not. In the following code I have made a kludge that works, but I think that it would be...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
14
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable =...
7
by: Wessel Troost | last post by:
Hi Group, How can you check if a variable is defined in C#? For example, "string s;" would declare an undefined variable. But how can I check in an "if" statement wether it's defined or not?...
3
by: PawelSokolowski | last post by:
Hi all, I have just switched from VC++ 6.0 to .NET and I am a bit confused. I am very upset that Class Wizard is gone. I am trying to add member variables to my dialog class. When I add control...
14
by: Matt | last post by:
I need to add the following variable into an SQL statement and not sure how to do it. strGCID needs to be inserted into the following statement: SQL = "SELECT tblContacts.* FROM tblContacts...
6
by: Vyoma | last post by:
This is quite a bit of problem I am facing, and I cannot point exactly where I am going wrong. I have been lurking around at several forums with regard to login and user authentication scripts and...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
3
by: Jack | last post by:
Hi everyone, I am stuck on something really stupid and simple. Here is what I am trying to do. Login, check the username and password against db and if it checks out then check the logged variable...
1
by: kraj123 | last post by:
Hi, How to Find ,if already a environment variable is set in hash table in perl. Actually i want to check if a environmental variable in perl script, which is present in oracle database has...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.