Question

i want to get matrix values, rows and coloumn from user input. So i'm implementing do while to do this: This is only for the rows:

do {
        printf ("Inserisci il numero di righe ( intero-positivo-diverso da 0): ");
        scanf ("%d",&righe);
    } while (righe<=0);

I want to check that user insert only integers. what can i do?

Was it helpful?

Solution 4

your code is correct. is the right way to do what you want...

OTHER TIPS

scanf returns an integer indicating how many "things" it successfully read. You can test this in a conditional to see if it got what you were looking for. Example:

const int result = scanf ("%d",&righe);
if (1 != result) {
  /* didn't get the 1 input we were looking for, do something about it! */
}

You'll want to distinguish between EOF and simply not an integer though.

Check the return value of scanf() it should return the number of converted strings, if it is != 1 the input couldn't be converted to an integer.

Reference :scanf

You are looking for this ctype.h

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top