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 9063373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:58:57+00:00 2026-06-16T15:58:57+00:00

Here is some C code trying simply to prevent the user from typing a

  • 0

Here is some C code trying simply to prevent the user from typing a character or an integer less than 0 or more than 23.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    const char *input;
    char *iPtr;
    int count = 0;
    int rows;

    printf("Enter an integer: ");
    scanf("%s", input);
    rows = strtol(input, &iPtr, 0);
    while( *iPtr != '\0') // Check if any character has been inserted
    {
        printf("Enter an integer between 1 and 23: ");
        scanf("%s", input);
    }
    while(0 < rows && rows < 24) // check if the user input is within the boundaries
    {
        printf("Select an integer from 1 to 23: ");
        scanf("%s", input);
    }  
    while (count != rows)  
    {  
        /* Do some stuff */  
    }  
    return 0;  
}

I made it halfway through and a small push up will be appreciated.

  • 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-16T15:58:58+00:00Added an answer on June 16, 2026 at 3:58 pm

    Use scanf("%d",&rows) instead of scanf("%s",input)

    This allow you to get direcly the integer value from stdin without need to convert to int.

    If the user enter a string containing a non numeric characters then you have to clean your stdin before the next scanf("%d",&rows).

    your code could look like this:

    #include <stdio.h>  
    #include <stdlib.h> 
    
    int clean_stdin()
    {
        int c;
        while ((c = getchar()) != '\n' && c != EOF)
            ;
        return 1;
    }
    
    int main(void)  
    { 
        int rows =0;  
        char c;
        do
        {  
            printf("\nEnter an integer from 1 to 23: ");
      
        } while (((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin()) || rows<1 || rows>23);
    
        return 0;  
    }
    

    Explanation

    1)

    scanf("%d%c", &rows, &c)
    

    This means expecting from the user input an integer and close to it a non numeric character.

    Example1: If the user enter aaddk and then ENTER, the scanf will return 0. Nothing capted

    Example2: If the user enter 45 and then ENTER, the scanf will return 2 (2 elements are capted). Here %d is capting 45 and %c is capting \n

    Example3: If the user enter 45aaadd and then ENTER, the scanf will return 2 (2 elements are capted). Here %d is capting 45 and %c is capting a

    2)

    (scanf("%d%c", &rows, &c)!=2 || c!='\n')
    

    In the example1: this condition is TRUE because scanf return 0 (!=2)

    In the example2: this condition is FALSE because scanf return 2 and c == '\n'

    In the example3: this condition is TRUE because scanf return 2 and c == 'a' (!='\n')

    3)

    ((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin())
    

    clean_stdin() is always TRUE because the function return always 1

    In the example1: The (scanf("%d%c", &rows, &c)!=2 || c!='\n') is TRUE so the condition after the && should be checked so the clean_stdin() will be executed and the whole condition is TRUE

    In the example2: The (scanf("%d%c", &rows, &c)!=2 || c!='\n') is FALSE so the condition after the && will not checked (because what ever its result is the whole condition will be FALSE ) so the clean_stdin() will not be executed and the whole condition is FALSE

    In the example3: The (scanf("%d%c", &rows, &c)!=2 || c!='\n') is TRUE so the condition after the && should be checked so the clean_stdin() will be executed and the whole condition is TRUE

    So you can remark that clean_stdin() will be executed only if the user enter a string containing non numeric character.

    And this condition ((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin()) will return FALSE only if the user enter an integer and nothing else

    And if the condition ((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin()) is FALSE and the integer is between and 1 and 23 then the while loop will break else the while loop will continue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get some form data from POST method. Here's the code
I am trying to reduce some code here. I will explain how I have
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
if (condition) { #lol. Some code here } else { header(Location:/);//i'm trying to redirect
I'm trying to learn some XML Parsing here and I've been given some code
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
Here, is my code. Just trying to wrap my head around some of the
I keep getting this error while trying to modify some tables. Here's my code:
I took some code from some questions here in SO as well as some
I'm just learning Haskell and I am trying to write some code that simply

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.