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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:12:43+00:00 2026-06-11T07:12:43+00:00

I have a C program where the user will be entering integers or doubles

  • 0

I have a C program where the user will be entering integers or doubles into the command line as input. Negative numbers are allowed, but I have the issue of when a user enters some thing like this:

1-1

It just gets parsed as being a one. I created a function to test whether or not the user input is a valid number, but I’m not sure how to catch instances like that, or the same goes for input like 1+1, 1)2, so on.

Here is the function I created:

int check_number(char *userInput) {
int i;
//check each character
for (i = 0; userInput[i] != '\0'; i++){
    if (isalpha(userInput[i])){
        printf("Invalid input.\n");
        return -1;
    }
}       
return 0;
}

What should I do to not allow any other random characters (besides letters, since those are already getting tested with isalpha) in the middle of user input?

  • 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-11T07:12:44+00:00Added an answer on June 11, 2026 at 7:12 am

    Basically you want the first character to be either a minus or a digit (and maybe a period) all subsequent characters should be digits or a period. But you will probably also want to count the periods to make sure there is only one. If you want to accept scientific notation as well you will need to deal with that as well.

    Checking for not isalpha is the wrong way around, you should check for the characters that are allowed.

    Basically something like:

    if (*userInput != '-' && !isdigit(*userInput))
      return -1;
    int periods = 0;
    while (*++userInput != 0) {
      if (*userInput == '.') {
        if (periods >= 1)
          return -1;
        ++periods;
        continue;
      }
      if (!isdigit(*userInput))
        return -1;
    }
    return 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program written in C using ncurses. It let user input and
I have a Win32 C++ program that validates user input and updates the UI
I am adding a feature to my program in which the user will have
Say I want a have a C program that does this: 1. User will
I have a program whereby the user will enter the url for a picture,
I have a program where user input is required, a user types in a
I have the following program flow: User fills in form My app generates a
I have done one program that allow user can listen to the music via
I have a program that allows the user to enter a level number, and
I have a program in which a user selects a row in a Datagrid

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.