0

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You are looking for this ctype.h

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.