Solved

How can i check is the input is integer?

Posted on 2001-08-19
7
543 Views
Last Modified: 2012-06-27
I Need to write an small problem using C not C++ under linux. It need to get the input from user, until the user enter 1234 then exit the program. my progam needs to be able to check if the user input is  not an integer value.So I use the loop for the user input and checking. my problems is my system only run right for the first time, for second time it don't ask the user to input the value, is there  something wrong on gets()?

here is part of my programs
//used to check if tinteger
int IsInteger (char * c)
{
 int length;
 int temp =1;
 int Result;
 int loop;
 
 //get the length of the string
 length = strlen(c);
 //loop=0;
 //loop thour each character
 for (loop =0 ;loop < length; loop ++)
 {
     //check the chars in the string is integer    
      if (!((c[loop] == '1') || (c[loop] == '2') || (c[loop] == '3') || (c[loop] == '4') || (c[loop] == '5') || (c[loop] == '6') || (c[loop] == '7') || (c[loop] == '8') || (c[loop] == '9') || (c[loop] == '0' )))
      {
           temp = 0;
      }
 }
/*
     if ( ((strcmp(c[loop], '0')) == 0) || ((strcmp(c[loop], '1')) ==0)|| ((strcmp(c[loop] , '2')) ==0) || ((strcmp(c[loop] , '3'))==0) || ((strcmp(c[loop] , '4'))==0) || ((strcmp(c[loop] ,'5'))==0) || ((strcmp(c[loop] ,'6'))==0) || ((strcmp(c[loop] ,'7'))==0) || ((strcmp(c[loop] ,'8'))==0) || ((strcmp(c[loop], '9'))==0))
     {    
          temp = 0;
          break;
     }*/
     return (temp);
}  

main()

          do
          {
               c1[0] = "\0";    
               printf("Enter first number: ");
               
               gets(c1);

               IsInt = IsInteger(c1);          if (IsInt == 0) printf("Invalid Input, Please enter INTEGER Number.\n");
                   
          }
          while (IsInt == 0);

0
Comment
Question by:tseyeh
7 Comments
 
LVL 1

Author Comment

by:tseyeh
ID: 6403166
for the main function portion, I forget to put the  one more outer loop for checking the user input equal to exit code. heres the more detail one


main()
   do
          do
          {
               c1[0] = "\0";    
               printf("Enter first number: ");
               
               gets(c1);

               IsInt = IsInteger(c1);          if (IsInt == 0) printf("Invalid Input, Please enter INTEGER Number.\n");
                   
          }
          while (IsInt == 0);

  while(c1=="1234");
0
 
LVL 6

Expert Comment

by:kotan
ID: 6403202
There is a topic for the C language, you should post the questiong to there.

I found that you have missed a word "break" in the for loop of isInteger().
You shouldn't campare string like c1 == "1234", instead of you should use strcmp().
However, there is a built in function for checking if the char is integer, which is isdigit(char) in ctype.h

I have rewrite the program for you, have a look..

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
   char c1[10];
   int isNo;
   int i;

   do {
      do {
         isNo = 1;
         printf("Enter first number: ");
         gets(c1);

         for (i = 0; i < (int) strlen(c1); i++)
         {
            if (!isdigit(c1[i]))
            {
               printf("Invalid Input. Please try again.\n");
               isNo = 0;
               break;
            }
         }
      } while (isNo == 0);
   } while (strcmp(c1, "1234"));

   return 0;
}

Best regards,
kotan
0
 
LVL 1

Author Comment

by:tseyeh
ID: 6403215
thanks...for u reply, but when i try to get the user input for second time, it give me an endless loop, is it becouse i need to to clear the buffer on c1, before i can using gets again???
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 33

Expert Comment

by:hongjun
ID: 6403221
>>is it becouse i need to to clear the buffer on c1, before i can using gets again???
You should include this before every gets() or getchar()
fflush(stdin);

hongjun
0
 
LVL 33

Accepted Solution

by:
hongjun earned 50 total points
ID: 6403223
The reason is that without the fflush, the RETURN key will be read in which is entered in the previous input. Thus we have to flush all input from the stdin before reading any characters or array of characters.

hongjun
0
 
LVL 1

Expert Comment

by:bitq
ID: 6409187
BOOL IsInteger(const char * szStr)
{
   int iTemp;
   char c;
   return 1 == sscanf(szStr,"%d%c",&iTemp,&c);
}
0
 
LVL 1

Author Comment

by:tseyeh
ID: 6413008
thanks for u answer, fflush is the function i actually looking for.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question