Home > Enterprise >  Restrict input to symbols and other numbers (с program)
Restrict input to symbols and other numbers (с program)

Time:11-05

I have such a program

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

int main() {
    float x, k;
    int choose;
    _Bool valid;

    do {
        
        valid = 1;

        printf("\nChoose variant: \n1 - count of nums\n2 - float number\n3 - e:\nYour choose: ");
        scanf(" %d", &choose);
        while(isdigit(choose) == 0) {
            printf("Please, choose number, not letter (number between 1 to 3): ");
            fflush(stdin);
            scanf(" %d", &choose);
        }

        while(choose > 3 || choose < 0) {
            printf("Please, choose correct option (number between 1 to 3): ");
            fflush(stdin);
            scanf(" %d", &choose);
        }

        if(choose == 1 || choose == 2 || choose == 3) valid = 0;

    } while (valid);


return 0;
}

I have such a program. I want to get the user to choose: 1, 2 or 3. I'm trying to put a check on characters and also on other numbers. I want the program to loop until I enter the correct one. But it does not work, I have already tried other methods to solve this problem, but it does not work for me. I have an idea that there is no cleaning here, maybe this is so?

I can also set a condition so that scanf is equal to one - this means that a character has been entered. But I want if the user enters "1gf ", for example, then the condition will also work, instead of continuing from 1.

Thank you very much in advance

CodePudding user response:

Rather than checking for a numeric entry the program might scan in a character to test and then convert to an integer as in the following code snippet.

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

int main()
{
    float x, k;
    int choice;
    char choose;
    int valid = 1;

    do
    {
        printf("\nChoose variant: \n1 - count of nums\n2 - float number\n3 - e:\nYour choose: ");
        scanf(" %c", &choose);

        if((choose > '9'))
        {
            printf("Please, choose number, not letter (number between 1 to 3): ");
        }

        if((choose < '1' || (choose > '3')))
        {
            printf("Please, choose correct option (number between 1 to 3): ");
        }

        if(choose == '1' || choose == '2' || choose == '3') valid = 0;

    }
    while (valid);

    choice = choose - '0';

    printf("The choice is %d\n", choice);

    return 0;
}

Following are some points to note.

  • The choose variable is defined as a character in lieu of an integer.
  • Testing is done over character ranges to determine if the entry is valid for this code.
  • The choice value is then determined simply by subtracting the integer value of the zero character from the entered character value.

Testing this out at the terminal resulted in the following sample output.

@Dev:~/C_Programs/Console/Choice/bin/Release$ ./Choice 

Choose variant: 
1 - count of nums
2 - float number
3 - e:
Your choose: e
Please, choose number, not letter (number between 1 to 3): Please, choose correct option (number between 1 to 3): 
Choose variant: 
1 - count of nums
2 - float number
3 - e:
Your choose: 4
Please, choose correct option (number between 1 to 3): 
Choose variant: 
1 - count of nums
2 - float number
3 - e:
Your choose: 2
The choice is 2

For sure, there are a multitude of ways the entries could be input and checked. This is just one way. Give it a try and see if it meets the spirit of your project.

CodePudding user response:

You cant do this in standard C you need to use some additional libraries. For example in Linux ncurses.

#include <ncurses.h>   
#include <string.h>

char *readstr(char *buff, size_t size, const char *allowed)
{
    size_t cpos = 0;
    int c;
    
    while ((c = getch()) != ERR && c != '\n' && (cpos   1) < size)
    {
        if(strchr(allowed, c)) buff[cpos  ] = c;
        else addstr("\b \b");
    }
    buff[cpos] = 0;
    return c == ERR ? NULL : buff;
}

int main (void) 
{
    char number[7];
    
    cbreak(); 
    echo();

    initscr();
    
    if(readstr(number, sizeof(number), "0123456789"))
    {
        printf("you have entered: `%s`\n", number);
    }
    else
    {
        printf("ERROR!!!!\n");

    }
    return 0;
}

CodePudding user response:

A scanset %1[123] will scan one character that is a 1, 2 or 3.
Another scanset %*[^\n] will scan and discard everything up to a newline.

#include <stdio.h>

int main ( void) {
    char choice[2] = "";
    int scanned = 0;

    do {
        printf("\nChoose variant: \n\t1 - count of nums\n\t2 - float number\n\t3 - e:\nYour choose: ");
        if ( 1 != ( scanned = scanf(" %1[123]", choice))) {
            if ( EOF == scanned) {
                fprintf ( stderr, "EOF scanf\n");
                return 0;
            }
            printf("Please, choose correct option (number between 1 to 3): ");
            scanf ( "%*[^\n]");
        }
    } while ( 1 != scanned);


return 0;
}
  • Related