2

I want to know how to make an error message if the user put more numbers than the max value of the array. (the numbers that the user enters should be in the same line)

#include <stdio.h>
#define N 10

int main() {
    int arr[10];
    int i;
    
    printf("Please enter %d numers\n", N);
    
    for (i=0; i<N; ++i) {
        scanf(" %d", &arr[i]);
    }
    
    for (i=0; i<N; ++i) {
        printf("%d ", arr[i]);
    }
    
    return 0;
}

For example in the code that I wrote, the code will just ignore more that 10 numbers, but I want to know how can I make instead an error massage. (is the same if he puts less numbers that I wanted?)

In other words how can I know how many numbers the user entered?

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 1
    Have you tried checking if `N` is larger than 10 and act accordingly? That's most basic knowledge... – Jabberwocky Jun 01 '22 at 14:29
  • 2
    You should check that `scanf` returns 1 (since it is reading 1 item). – Ian Abbott Jun 01 '22 at 14:31
  • 2
    To check for no more input after reading `N` numbers, you could call `scanf(" ");` to eat up any remaining white space characters (including the newline) after the final number, and then check `feof(stdin)` returns non-zero. Checking for end of file is probably unfriendly for interactive use. Better just to ignore any excess input. – Ian Abbott Jun 01 '22 at 14:38
  • 1
    @AndreasWenzel its supposed to be in the same line. – Little Monkey Jun 01 '22 at 16:23
  • 1
    So you want an error message if the number of numbers that the user entered is higher or lower than `10`? You want an error message in both cases? So you want the number of numbers that the user enters to be exactly `10`? Did I understand you correctly in that respect? – Andreas Wenzel Jun 01 '22 at 16:27
  • @AndreasWenzel yes exactly – Little Monkey Jun 01 '22 at 16:31
  • This is, in general, hard to do with interactive devices except if you receive an eof. See [this answer](https://stackoverflow.com/a/26557243/2472827). Your requirement that it's all on one line is important, as it allows line-buffering. – Neil Jun 01 '22 at 17:25

1 Answers1

2

The problem with using the function scanf is that it will ignore whitespace characters. Since the newline character is a whitespace character, this means that scanf will ignore any newline characters. Therefore, if you want to enforce that all numbers entered are in the same line, then you should not be using the function scanf.

In order to read a single line of input, I recommend that you use the function fgets. After reading one line of input, you can use the function strtol to attempt to convert the characters to numbers.

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

#define N 10

int main( void )
{
    int arr[N];
    char line[500], *p;

    //prompt user for input
    printf( "Please enter %d numbers: ", N );

    //attempt to read one line of input
    if ( fgets( line, sizeof line, stdin ) == NULL )
    {
        fprintf( stderr, "Input error!\n" );
        exit( EXIT_FAILURE );
    }

    //attempt to read 10 numbers
    p = line;
    for ( int i = 0; i < N; i++ )
    {
        char *q;

        arr[i] = strtol( p, &q, 10 );

        if ( q == p )
        {
            printf( "Error converting #%d!\n", i + 1 );
            exit( EXIT_FAILURE );
        }

        p = q;
    }

    //verify that the remainder of the line contains nothing
    //except whitespace characters
    for ( ; *p != '\0'; p++ )
    {
        if ( !isspace( (unsigned char)*p ) )
        {
            printf( "Error: Non-whitespace character found after 10th number!\n" );
            exit( EXIT_FAILURE );
        }
    }

    //print results
    for ( int i = 0; i < N; i++ )
    {
        printf( "%d ", arr[i] );
    }
    
    return 0;
}

This program has the following behavior:

Please enter 10 numbers: 1 2 3 4 5
Error converting #6!
Please enter 10 numbers: 1 2 3 4 5 6 7 8 9 10 11
Error: Non-whitespace character found after 10th number!
Please enter 10 numbers: 1 2 3 4 5 6 7 8 9 10   
1 2 3 4 5 6 7 8 9 10 

Note that my code above has the following issues:

  1. It will not check whether the line was too long to fit into the input buffer (i.e. longer than 500 characters).

  2. It will not check whether the number the user entered is representable as an int (i.e. whether it is larger than INT_MAX, which is 2,147,483,647 on most platforms).

If you also want to address these issues, that I suggest that you take a look at my function get_int_from_user in this answer of mine to another question.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39