?
Solved

How can i check is the input is integer?

Posted on 2001-08-19
7
Medium Priority
?
567 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
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

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!

Question has a verified solution.

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

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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

862 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