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

  • Home
  • SEARCH
  • 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 9117827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:59:19+00:00 2026-06-17T04:59:19+00:00

I am trying to create some code that will only accept valid that as

  • 0

I am trying to create some code that will only accept valid that as user input, like no integers when asking for name etc…
I have tried this so far,

    if(!gets(newCust.name))
    {
        printf("invalid data\n");
        menu();
    }

However, since name is of type string, when user inputs integers it seems to be valid. Is there any other simple option I could use ?

  • 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-17T04:59:20+00:00Added an answer on June 17, 2026 at 4:59 am

    Validating input (especially from C) can be quite a bit of work. In general, the way I would do it is to read the buffer (you should maybe use fgets( …, stdin) instead of gets to avoid buffer overflows). After reading the buffer (no matter what the input is), then write the code to validate the data that is in the buffer.

    For example, you could use strtol to read integer values from the buffer. That would allow you to determine if the buffer has an integer (and only an integer).

    The following is the general idea:

    char buf[1024];
    int val;
    char *endptr;
    
    printf( "Enter something..." );
    if ( fgets( buf, sizeof buf, stdin )) {
       val = strtol( buf, &endptr, 10 );
    
       // check for some of the possible errors
       if ( val == LONG_MIN )
          printf( "Underflow error (%d)\n", errno );
       else if ( val == LONG_MAX )
          printf( "Overflow error (%d)\n", errno );
       else if ( endptr == buf )
          printf( "No digits provided\n" );
       else if ( *endptr != '\0' && *endptr != '\n' )
          printf( "Something besides an integer was given (%u)\n", *endptr );
       else
          printf( "The value is apparently valid\n" );
       printf( "val = %d\n", val );
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a framework that keeps some common code I use around
I am trying to run some javascript code that is created by my codebehind
I am trying to create DLL out of my C# code, I found some
Some context for this code, what I am trying to do is create a
I'm trying to create some classes that allow me to retrieve and manipulate a
I am trying to create a simple batch file that will replace a line
I'm trying to create a sub-class of the JButton component that will enable or
I'm trying to create a custom template tag that only renders a block of
I'm writing some pretty CPU-intensive, concurrent numerical code that will process large amounts of
I have a PHP question. I am trying to alter some code that a

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.