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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:19:48+00:00 2026-06-06T16:19:48+00:00

The function is used to validate the input. It prompts the user for a

  • 0

The function is used to validate the input. It prompts the user for a numeric value (great or equal to 0 ) until it meets the coditions. if any character input precedes or follows the number, the input is to be treated as invalid.The required output is:

Enter a positive numeric number: -500
Error! Please enter a positive number:45abc
Error! Please enter a number:abc45
Error! Please enter a number:abc45abc
Error! Please enter a number:1800

Well, it seems easy:

#include <stdio.h>
main() {
    int ret=0;
    double num;
    printf("Enter a positive number:");
    ret = scanf("%.2lf",&num);

    while (num <0 ) {
        if (ret!=1){
            while(getchar()!= '\n');
            printf("Error!Please enter a number:");
        }
        else{
            printf("Error!Please enter a positive number:");
        }
        ret = scanf("%.2lf",&num);
    }
}

however, my code keeps put out Error!Please enter a number: regardless types of input. Any advice?

  • 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-06T16:19:49+00:00Added an answer on June 6, 2026 at 4:19 pm

    I think you’ll have problems doing the validation you want using just scanf(). You’ll do better to first scan in a string and then convert it to a numeric. But scanf() is dangerous for scanning in char strings, since its input length is not limited and you have to provide it a pointer to a finite-length input buffer. Better to use fgets(), which allows you to limit the input buffer length.

    #include <stdio.h>
    int main(int argc, char **argv)
    {
        double num=-1;
        char input[80]; // arbitrary size buffer
        char* cp, badc; // badc is for detecting extraneous chars in the input
        int n;
        printf("Enter a positive number:");
        while (num < 0)
        {
            cp = fgets(input, sizeof(input), stdin);
            if (cp == input)
            {
                n = sscanf(input, "%lf %c", &num, &badc);
                if (n != 1) // if badc captured an extraneous char
                {
                    printf("Error! Please enter a number:");
                    num = -1;
                }
                else if (num < 0)
                    printf("Error! Please enter a POSITIVE number:");
            }
        }
    
        printf("num = %f\n", num);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to validate user input by using the QString::toDouble() function. The documentation says
I used the following code: <script type=text/javascript> function validate() { if ((document.changepass.newpassword.value != '')
Template code is not compiled until the template function is used. But where does
What is the function used to create taxonomy terms in Drupal from the code?
The first .click function is used to add an element(div) in a container and
What Javascript function is used to check for a certain plugin? Such as for
When the document(uri) function is used to load another document within the XSLT, where
I have one javascript function that used to display the menu and content in
In my parent jsp page I've used: function popUpClosed() { window.location.reload(); } And in
I am using following code to get bitmap from url. This function is used

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.