?
Solved

How can i check is the input is integer?

Posted on 2001-08-19
7
Medium Priority
?
558 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 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