Writing C program for user to input 50 or less numbers

偶尔善良 提交于 2021-01-07 01:31:54

问题


I am trying to write a C program that has to ask the user for input to take in 50 or less numbers. My problem is the "or less" numbers. How do I get the program to know when the user wants to stop entering numbers?


回答1:


Here is the answer to your question:-

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
void main()
{
    int a[100],n,i;
    printf("Enter the no of inputs u want to give (It must be less than or eqaual to 50)\n");
    scanf("%d",&n);
    printf("Enter the nos \n");
    if(n<=50)
    {
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
}
    }
    else
        printf("Invalid Input \n");
        getch();
}



回答2:


The simplest way to handle user input of some unknown number of inputs, is simply to read all input with fgets() and allow the user to press Enter on an empty-line when done with input. You also must check for a manual EOF because it is valid for the user to press Ctrl + d (or Ctrl + z on windows) to cancel input. (you generally want to exit gracefully if the user does that)

Reading with fgets() you read input into a character array. All line-oriented input functions (e.g. fgets() and POSIX getline()) read and include the '\n' generated by the user pressing Enter following each input in the buffer they fill. So just check if the first character in the buffer is the '\n' character to see if the user is done with input and pressed Enter on an empty-line.

A short example:

#include <stdio.h>

#define MAXC 1024       /* if you need a constant, #define one (or more) */
#define MAXI   50

int main (void) {
    
    char line[MAXC];    /* buffer to hold user input */
    int array[MAXI];    /* array to hold up to MAXI integer values */
    size_t n = 0;       /* counter for number of integers in array */
    
    printf ("\nEnter up to %d integers, Press [ENTER] on empty-line to end:\n\n", MAXI);
    
    while (n < MAXI) {                                  /* while array not full */
        printf (" array[%2zu]: ", n);                   /* prompt for input */
        
        if (fgets (line, MAXC, stdin) == NULL) {        /* if manual EOF, quit */
            puts ("(user canceled input)");
            return 0;
        }
        if (line[0] == '\n')                            /* if Enter on empty-line, done */
            break;
        
        if (sscanf (line, "%d", &array[n]) == 1)        /* if valid integer */
            n += 1;                                     /* increment counter */
        else    /* otherwise, handle error */
            fputs ("  error: invalid integer input.\n", stderr);
    }
    
    puts ("\narray content:\n");
    for (size_t i = 0; i < n; i++)
        printf (" array[%2zu] : %d\n", i, array[i]);
}

(note: how the loop condition while (n < MAXI) ensures you never write more integers to your array than it can hold)

Example Use/Output

The program fully validates the integer input and handles any non-integer input as well by simply informing the user though an error message and then re-prompting for the input:

$ ./bin/fgets_n_integers

Enter up to 50 integers, Press [ENTER] on empty-line to end:

 array[ 0]: 10
 array[ 1]: 20
 array[ 2]: Three Hundred Twenty-Six!!!
  error: invalid integer input.
 array[ 2]: 30
 array[ 3]: 40
 array[ 4]: 50
 array[ 5]: Are we done yet?, No Press [Enter] on an empty-line!
  error: invalid integer input.
 array[ 5]: 60
 array[ 6]: 70
 array[ 7]: 80
 array[ 8]: 90
 array[ 9]:

array content:

 array[ 0] : 10
 array[ 1] : 20
 array[ 2] : 30
 array[ 3] : 40
 array[ 4] : 50
 array[ 5] : 60
 array[ 6] : 70
 array[ 7] : 80
 array[ 8] : 90

Look things over, give it a try and let me know if you have further questions.




回答3:


If I understand correctly, you need to allow user to enter inputs not more than 50 times. You can simply use a variable to count the number of inputs like:

int main()
{
    int input, count=0;
    printf("Enter not more than 50 inputs\n");
    while (count < 50)
    {
      printf("Remaining inputs: %d\n", 50-count);
      scanf("%d", &input); // I just use an integer input for this answer
      printf("Entered: %d\n", input); // frame your own logic to handle the inputs
      count++;
    }
    printf("Done!");
    return 0;
}


来源:https://stackoverflow.com/questions/65262421/writing-c-program-for-user-to-input-50-or-less-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!