[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can i check is the input is integer?

Posted on 2001-08-19
7
Medium Priority
?
564 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

  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 …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

649 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