-1

I have a problem with my code:
When I write any input different from 1,2,3,4 the output is

Inserire il numero dei giocatori 
inserire un numero valido
Inserire il numero dei giocatori 
inserire un numero valido
Inserire il numero dei giocatori 

How can I fix it?


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>

int controll_num(){

    int controll=0;
    int players;
    char c;
    do{
        printf("Inserire il numero dei giocatori \n");
        c=getc(stdin);

        switch (c){

            case 49:
                players=1;
                controll=1;
                break;

            case 50:
                players=2;
                controll=1;
                break;

            case 51:
                players = 3;
                controll=1;
                break;

            case 52:
                players = 4;
                controll=1;
                break;

            default:
                printf("inserire un numero valido\n");
        }
    }while(controll==0);
    return players;
}

int main(){

    controll_num();

    return 0;
}

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    You have described exactly what your program is doing. If you want it to do something else, you need to modify it. What do you want it to do and how do you think you need to modify it? (Please edit your question accordingly) – Dominique Jan 24 '23 at 10:30
  • Welcome to Stack Overflow. In your own words, where the code says `c=getc(stdin);`, **exactly what do you think this means?** (Hint: what might the `c` in `getc` stand for? Hint: if the input has commas in it, how do you expect those to get handled?) – Karl Knechtel Jan 24 '23 at 10:34
  • Riccardo Ricci: Change `printf("inserire un numero valido\n");` --> `printf("inserire un numero valido %d\n", c);` and the answer will become clear. – chux - Reinstate Monica Jan 24 '23 at 11:12

2 Answers2

1

Instead of getc use scanf as for example

scanf( " %c", &c );

Pay attention to the leading space in the format string. It allows to skip white space characters as for example the new line character '\n' that is placed in the input buffer by pressing the Enter key.

As for getc then it can read white space characters.

Also instead of using magic numbers like 49 as case labels

case 49:

use characters like

case '1':

This will make your code more readable.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You need to check if the value is digit and your switch case is not needed at all.

int controll_num(void){

    int players;
    char c;
    while(1)
    {
        printf("Inserire il numero dei giocatori \n");
        c=getc(stdin);

        if(isdigit((unsigned char)c)) 
        {
            players = c - '0';
            break;
        }
        printf("inserire un numero valido\n");

    };
    return players;
}

int main(void)
{
    printf("Number of players %d\n", controll_num());
}

https://godbolt.org/z/sf7nxE7cx

0___________
  • 60,014
  • 4
  • 34
  • 74