Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
void FillArray(int m[35])
{
    int i;
    for (i = 0; i <= 34; i++)
    {
        printf("Enter Mark for Element[%d] :", i);
        scanf("%d", &m[i]);
        while (m[i]<=1 || m[i]>=7)
        {
        printf("\nError! Mark should in range of (2 to 6).\n\n");
        printf("Enter Mark again for Element[%d] :", i);
        scanf("%d", &m[i]);
        }
    }
}



in this code i m taking nubers between 2 - 6 but when i enter a letter sticking to code which parameter i need to just use numbers mean not allow letters or characters thank you
Posted
Updated 11-Dec-13 0:31am
v2
Comments
CPallini 11-Dec-13 8:01am    
Why are you using while? if would be enough (just curiosity).

scanf can't prevent the user typing anything: all it does is read what the user typed and try to process it according to the format you provide and return a value which says if it passed or failed.
If you need to prevent the user typing invalid characters, you need to use your operating system / compiler specific functions to read each character as it is typed and accept or reject it at that point.
 
Share this answer
 
Comments
ivaylov 11-Dec-13 6:49am    
can you give one sample if user input letter or character it gives error and re-enter imput again
OriginalGriff 11-Dec-13 7:29am    
Character-by-character checking is not parts of "standard C" - it's implementation depends on the compiler / OS you are using, so no I can't.

The only solution which uses standard C functions would be to read the whole string as a string, process it to check it is entirely valid, and report an error - but I don;t think that is what you are looking for.
AS Griff says you need to go to a lower level than scanf. I can't help mentioning how useful Google is. Try this and ignore the 2 vote:

Rread a number input from the keyboard and then put each of its components in an array[^]
 
Share this answer
 
v2
Quote:
scanf("%d", &m[i]);
That is not the correct usage of scanf: you should always check its return value, that is:
C
if ( scanf("%d", &m[i]) != 1 )
{
  // handle error.
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900