Solved

How can i check is the input is integer?

Posted on 2001-08-19
7
512 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 33

Expert Comment

by:hongjun
Comment Utility
>>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
Comment Utility
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
Comment Utility
BOOL IsInteger(const char * szStr)
{
   int iTemp;
   char c;
   return 1 == sscanf(szStr,"%d%c",&iTemp,&c);
}
0
 
LVL 1

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now