Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:15:30+00:00 2026-06-14T04:15:30+00:00

I have written following code to get an integer from keyboard. It will prompt

  • 0

I have written following code to get an integer from keyboard.
It will prompt an Error message until you give a valid integer value (either negative or positive ).

One condition is It has to check every possible test cases

like:

-3.2 
5.0
984237.4329
0.343
.434
12344.
adfs34
233adds
3892710492374329
helloIamNotainteger 

For all of this tests it should fail.It will pass only for int >=INT_MIN && int <=INT_MAX
value.

My Running code is :

#include<stdio.h>
#include<limits.h>

int min=INT_MIN;
int max=INT_MAX;

int main()
{
        char str[50],c; //I have taken size 50 please ignore this 
        int check;
 do
 {
        int flag=1,i=0,j=0,num=0;
        check=0;
        printf("Enter an Integer : ");
        while((c=getchar())!='\n')
                str[i++]=c;
        if(str[0] == '-')
        {
                flag = -1;
                j++;
        }
        for(;j<i;j++)
        {
                if(str[j] >= '0' && str[j] <= '9')
                        num=(str[j]-'0') + num*10;
                else
                        break;

        }
        if(j<i)
        {
          printf("Not an Integer, Please input an integer \n");
        }
        else if(num < min || num >max)
        {
          printf("Integer is out of range,Please input an integer \n");
        }
        else
        {
                num *=flag;
                printf("The given number is : %d\n",num);
                check=1;
        }
 }while(check == 0);
        return 0;

}

One example : For values like this.
83429439803248832409 (It’s integer but it should fail because of range )but it passes and give some other integer value.

How to solve this within my code or any better idea to implement getInt() ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:15:32+00:00Added an answer on June 14, 2026 at 4:15 am

    Here is the working code : please see this , I did that wihtout strtol and it satisfies all conditions and takes only int

        #include<stdio.h>
        #include<limits.h>
        #include<string.h>
    
        int main()
        {
                int num;
                char str[500],c;
                int check;
                char max[12];
                char min[12];
                sprintf(max,"%d",INT_MAX);
                sprintf(min,"%d",INT_MIN);
         do
         {
                int flag=1,i=0,j=0;
                num=0;
                check=0;
                printf("Enter an Integer : ");
                while((c=getchar())!='\n')
                        str[i++]=c;
                str[i]='\0';
                if(str[0] == '-')
                {
                        flag = -1;
                        j++;
                }
                for(;j<i;j++)
                {
                        if(str[j] >= '0' && str[j] <= '9')
                                check = 1;               
                    else
                    {
                            check = 0;
                            break;
                    }
    
            }
            if(check == 0)
            {
                    printf("Not an Integer, Please input an integer \n");
            }
            /************Start of checking integer range **************/
            else
            {
                    if( (flag == -1 && (strlen(str) > strlen(min))) || 
                        (flag == 1 && (strlen(str) > strlen(max))) )
                    {
                            check = 0;
                            printf("Integer is out of range, \n");
                    }
                    else if(flag == -1 && (strlen(str) == strlen(min)) )
                    {
                            i=0;
                            while(min[i]!='\0')
                            {
                                    if (str[i] > min[i])
                                    {
                                            check = 0;
                                            printf("Integer is out of range \n");
                                            break;
                                    }
                                    i++;                        }
                            if(check == 1)
                            {
                                    for(j=1;j<strlen(str);j++)
                                            num=(str[j]-'0')+num*10;
                                    num *=flag;
                                    printf("The given number is : %d\n",num);
                            }
                    }
    
                    else if(flag == 1 && (strlen(str) == strlen(max)) )
                    {
                            i=0;
                            while(max[i]!='\0')
                            {
                                    if (str[i] > max[i])
                                    {
                                            check = 0;
                                            printf("Integer is out of range\n");
                                            break;
                                    }
                                    i++;
                            }
                            if(check == 1)
                            {
                                    for(j=0;j<strlen(str);j++)
                                            num=(str[j]-'0')+num*10;
                                    num *=flag;
                                    printf("The given number is : %d\n",num);
                            }
                    }
                    else
                    {
                    for(j=0;j<strlen(str);j++)
                            num=(str[j]-'0')+num*10;
                    num *=flag;
                    printf("The given number is : %d\n",num);
                    }
            }
            /************End of checking integer range ****************/
     }while(check == 0);
    
    return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: //get the user from the DB var tmpuser
I have written following code to get JSON result from webservice. function SaveUploadedDataInDB(fileName) {
I have written a following code to get just the file name without extension
I have written following code to get location name package demo.gps.locname; import java.io.IOException; import
I have written the following code, but continually get a 'non-static method getText() can
I have written the following C code to get in a list of strings
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
i have written the following code class Program { static void Main(string[] args) {
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.