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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:36:19+00:00 2026-06-13T03:36:19+00:00

i want to do adding double value validation to my program. For ex: When

  • 0

i want to do adding double value validation to my program.

For ex: When user input string, it gives error “Please enter an integer instead of string.”

i also want that “Please enter an integer instead of double value.”

How can i apply this?

/*
 * HW3-3.c
 *
 *  Created on: Oct 21, 2012
 *      Author: mert
 */


 #include <string.h>
 #include <stdio.h>

  void checkTriangle(char *s1,char *s2,char *s3)
  {
    int i;
    int found_letter = 0;
    int len = strlen(s1);
    int len2 = strlen(s2);
    int len3 = strlen(s3);

    for( i = 0; i < len; i++)
    {
        if(s1[i] < '0' || s1[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        }
    }


    for( i = 0; i < len2; i++)
    {
        if(s2[i] < '0' || s2[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        }
    }


    for( i = 0; i < len3; i++)
    {
        if(s3[i] < '0' || s3[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        }
    }

    if(found_letter) // value 0 means false, any other value means true
        printf("Please enter an integer instead of string.");
    else
    {
            int side1 = atoi(s1);
            int side2 = atoi(s2);
            int side3 = atoi(s3);

            if ((side1 + side2 > side3 && side1 +  side3 > side2 && side2 + side3 > side1) && (side1 > 0 && side2 > 0 && side3 > 0))
                 {
                // Deciding type of triangle according to given input.
                  if (side1 == side2 && side2 == side3)
                      printf("EQUILATERAL TRIANGLE");
                  else if (side1 == side2 || side2 == side3 || side1 == side3)
                      printf("ISOSCELES TRIANGLE\n");
                  else
                      printf("SCALENE TRIANGLE \n");
                }
                 else
                     printf("\nTriangle could not be formed.");
    }
  }


  int main(void)
  {

      char s[32], s2[32], s3[32];

      printf("Please enter sides of triangle");
      printf("\nPlease enter side 1:");
      gets(s);
      printf("Please enter side 2:");
      gets(s2);
      printf("Please enter side 3:");
      gets(s3);

      checkTriangle(s,s2,s3);
  }
  • 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-13T03:36:21+00:00Added an answer on June 13, 2026 at 3:36 am

    You could try parsing the input and check the results. Note also that it is convenient to use a general function and not repeat three times the validation:

    /*
        Returns a positive integer if a positive integer was read from input.
        It will return -1 if the input is not a number,
        -2 if it was not an integer,
        -3 if it was not strictly positive.
    
        Bugs: "-0" is recognized as "not a number" instead of "not positive"
              "0185" is recognized as 185 instead of "not a (proper) number"
    */
    
    int askValue(char *message)
    {
        int val;
        char s[32];
        printf("%s: ", message);
    
        // Read string
        fgets(s, sizeof(s), stdin);
    
        // Trim string. fgets will return the \n.
        if (0 == strlen(s))
            return -1;
        s[strlen(s)-1] = 0x0;
    
        // Parse as an integer. 0 means it was either "0", or garbage
        if (0 == atoi(s))
        {
            if ('0' == s[0])
                val = 0; // It was a "0" (or maybe "0.17" or "0x18")
            else
                return -1; // Error: it was not a number
        }
        // Then parse as a float. "0.17" will give 0.17 which is not 0.00
        if ((double)val != atof(s))
        {
            // Error: the string checks as a floating point value
            return -2;
        }
        if (val <= 0)
            return -3;
        return val;
    }
    
    void complain(int val)
    {
        switch(val)
        {
            case -3: printf("Value must be greater than 0\n"); break;
            case -2: printf("Value must be integer\n"); break;
            case -1: printf("Value must be a number\n"); break;
        }
    }
    
    int main()
    {
        int side1, side2, side3; // We could use a vector: side[3]
    
        while ((side1 = askValue("Please enter side 1")) < 0)
            complain(side1);
        while ((side2 = askValue("Please enter side 2")) < 0)
            complain(side2);
        while ((side3 = askValue("Please enter side 3")) < 0)
            complain(side3);
    
        ...
    }
    

    This is a different implementation, more strict. It returns the integer value if it is positive, -2 if it was a negative integers, and -1 in all other cases (garbage and floats).

    It still has a false negative for “-0”, which parses as “0”.

    int askValue(char *message)
    {
        int val;
        char s[32], c[32];
        printf("%s: ", message);
    
        // Read string
        fgets(s, sizeof(s), stdin);
    
        // Parse as an integer. 0 means it was either "0", or garbage
        val = atoi(s);
    
        // Now produce the expected input for val, included final \n
        snprintf(c, sizeof(c), "%d\n", val);
    
        if (strcmp(c, s))
            return -1;
        if (val < 0)
            return -3;
        return val;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update a column by adding a new value alongside the old
When adding this code to a simple calculator program I receive the error message
I want to calculate a new date by adding a number of months to
I want to overload the + operator for adding segments together to form a
When adding a float attribute to my opengl fragment (i want to find to
I want to create a JToolBar without adding it into any JFrame window. If
I want to remove a css class before adding a new class to an
I want to add and remove div element using jquery and adding of div
I am adding the LinearLayout(child view) to another LinearLayout(parentview) programatically here I want to
I have a SVG file that I want to extend by adding onclick handlers

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.