Home > Blockchain >  c programming: Error handling. How do I stop characters from being entered into program
c programming: Error handling. How do I stop characters from being entered into program

Time:11-28

I'm new to programming so this may be a simple answer.

I have just recently started learning c and am wondering how I can exclude characters from being entered as a value in my programs.

Is there a way through the scanf funtion to recognise the input as a character and then write a printf to show an invalid value message? It would be more recognising the character then printing the message im concerned with.

Edit: So as asked, the below is my code for a program that first reads five numbers(each between 1 and 30).For each number read, the program should print a line containing that number of adjacent asterisks.

For this, if I enter a number value it causes the program to stop working. So if i could add a way to create "Try again" message or something similar when they are entered, this will stop it from having errors.

#include <stdio.h>

int main(void)
{
  int number1 = 0; int counter;
  int sentinelcount = 1;


  printf("Please enter 5 values, between 1 and 30");

  while (sentinelcount <= 5) {

    
        printf("\n\nEnter number: \n"); /*prompt*/
        scanf_s("%d", &number1); /*read an interger*/
        sentinelcount  ;

        if (number1 < 1 || number1 > 30)
        {
            sentinelcount--;
            printf("\nWrong Value\n");
        }

    
    
        if (number1 < 1 || number1 > 30) 
        {
            printf("Enter within correct value range: 1 - 30! ");
        }
        else if (number1 >= 1 || number1 <= 30) 
        {
            printf("Number of asterisks:\n");
                  for (counter = 1; counter <= number1; 
                       counter  ) 
                  {
                  printf("*");
                  }
        }

}
return 0;

Thanks,

CodePudding user response:

How do I stop characters from being entered into program

Short of some magic hand that prevent typing in non-numeric or a limited key board, the better approach is not to stop characters from being entered, but accept them as input and then detect invalid input.

I recommend to consume invalid input and alert the user of the issue.


A good first attempt is to read a line of input with fgets() into a string.

Test for input, conversion success, extra junk and range.

char buf[80];
if (fgets(buf, sizeof buf, stdin)) {  // TBD deal with lines longer than 79

If a line was read, process it with strtol(), sscanf(), etc. Use "%n" to detect where scanning ended. Perform error checking.

  int num;
  int n;
  // If an integer was parsed with no trailing junk and in range ...
  if (sscanf(buf, "M %n", &num, &n) == 1 && buf[n] == 0 && 
      (num >= 1 && num <= 30)) {
    Oh_Happy_Day(); // TBD code
  } else {
    Invalid_input(): // TBD code
  }

CodePudding user response:

if you want to exclude characters from being in your code you can use something like this:

unsigned long long answer1,answer2,answer3,c;


scanf("%*[^0123456789]%llu%*[^0123456789]%llu%*[^0123456789]%llu",&answer1,&answer2,&answer3);


printf("%lld %lld %lld",answer1,answer2,answer3);


return 0;

and if you want to print characters you shouldn't scan characters like this: scanf("%d"&a) instead you scan them with this:scanf("%c",&a) and the same point stands in print. but with this you can only scan one character at a time and if you want to scan more than that use more %c in the scanf and printf

  •  Tags:  
  • c
  • Related