Brandon Bustamante
Brandon Bustamante

Reputation: 11

How do I only accept numbers in C and block the user from entering letters and special characters?

I just need to figure out how to give an error is the user enters anything that is not number. I already set the values of the code that cannot be passed or be gone under.

I just need to only accept numbers: if a letter or any type of special character is entered I want the program to just cancel itself. How can I do this?

#include <stdio.h>
#include <math.h>

int main(void) {
    float base, height;
    float area;

    printf("Please enter the value of base of the triangle: \n");
    scanf ("%f", &base);
    if(base<.5)
        printf("Invalid Input\n");

    while (base<.5)
        return 0;

    if(base>100) 
        printf("Invalid Input\n");

    while(base>100)
        return 0;

    printf("Please enter the value of height of the triangle:\n");
    scanf("%f", &height);

    if(height<1)
        printf("Invalid Input\n");

    while(height<1)
        return 0;

    if(height>75)
        printf("Invalid Input\n");

    while (height>75)
        return 0;

    area = base * height/2;

    printf("The area of the triangle for base:  %f and height:  %f is %f \n", base,
            height , area );

    return 0;
}

Upvotes: 1

Views: 8946

Answers (3)

Matteo Corti
Matteo Corti

Reputation: 494

I think that you could read the input character by character and then check if it's a number and if it isn't you show your error message.

#include <stdio.h>
 #include <math.h>


int main(void)
{
    float base, height;
    float area;



printf("Please enter the value of base of the triangle: \n");
char c='\n';
char success=0;
base=0;
char dot=0;
do{
scanf ("%c", &c);
if((c>=48)&&(c<=57)){
  if(dot==0)
    base=base*10+(c-48);
  else{
    base+=(c-48)/dot;
    dot*=10;
  }
}
else
 if((c=='.')&&(dot==0))
     dot=10;
 else
   if(c!='\n')
     success=1;

}while((c!='\n')&&(succes==0));


if(success!=0) return -1; //error we break out
if(base<.5) 
printf("Invalid Input\n");
while (base<.5)
return 0;


if(base>100) 
printf("Invalid Input\n");
while(base>100)
    return 0;


printf("Please enter the value of height of the triangle:\n");
c='\n';
 success=0;
height=0;
 dot=0;
do{
scanf ("%c", &c);
if((c>=48)&&(c<=57)){
  if(dot==0)
    height=height*10+(c-48);
  else{
    height+=(c-48)/dot;
    dot*=10;
  }
}
else
 if((c=='.')&&(dot==0))//check if is the first time the user insert a dot
     dot=10;
 else
   if(c!='\n')
     success=1;

}while((c!='\n')&&(succes==0));


if(success!=0) return -1; //error we break out


if(height<1)
printf("Invalid Input\n");

while(height<1)
return 0;

if(height>75)
printf("Invalid Input\n");

while (height>75)
return 0;

area = base * height/2;

printf("The area of the triangle for base:  %f and height:  %f is %f \n", base,
height , area );

return 0;


}

Upvotes: 0

user545199
user545199

Reputation:

scanf returns the number of variables that have been matched and filled with values succesfully. just do:

int result = scanf ("%f", &base);
if(result != 1)
{
    ...//handle error
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You cannot block the user from entering whatever he or she wants, but you can use the return value of scanf to decide if a valid value has been entered, and prompt the user for a correct input:

float base;
do {
    printf("Please enter the value of base of the triangle: \n");
} while (scanf ("%f", &base) != 1 || base < .5 || base > 100);

This loop will continue until all three conditions are met:

  • scanf has returned exactly one item,
  • The value provided by scanf is greater than or equal to 0.5, and
  • The value provided by scanf is less than or equal to 100

Upvotes: 2

Related Questions