annie
annie

Reputation: 23

Counting Number Of User Input in C Program

printf("Enter number of patients:");
int numberOfInputs = scanf("%d", &patients);

if (numberOfInputs != 1) {
  printf("ERROR: Wrong number of arguments. Please enter one argument d.\n");
}

I am asking the user to input one number as an argument, but would like to print out a statement if the user does not input anything or puts in more than one input. For example, once prompted with "Enter number of patients:", if the user hits enter without entering anything, I would like to print out a statement. The code above is what I have been specifically tinkering around with it for the past couple hours as a few previous posts on this site have suggested but when I run it in terminal, it does not work. Any suggestions? Thank you in advance, and all advice is greatly appreciated!

Upvotes: 1

Views: 3938

Answers (2)

Zoltán
Zoltán

Reputation: 708

This looks super over-complicated, but it basically splits the input, checks it to be exactly one and than checks it to be an integer (and converts it). It works fine in loop as well and handles empty input. I'm sure there are more elegant solutions to this problem, it's just a suggestion.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

int getNumberOfInput(char* str);
bool isNumber(char* str);

int main()
{
    char str[512];
    while(1)
    {
        printf("Enter text: ");
        fgets(str, 512, stdin);

        int numberOfInput = getNumberOfInput(str);

        if ( numberOfInput == 0 )
            printf("You must give an input\n");
        else if ( numberOfInput > 1 )
            printf("You have to give exactly one input\n");
        else
        {
            if (!isNumber(str))
                printf("The input is not an integer\n");
            else
            {
                int input = atoi(str);
                printf("input: %d\n", input);
            }
        }
    }
    return 0;
}

int getNumberOfInput(char* str)
{
    char* word = strtok(str, " \t\n\v\f\r");
    int counter = 0;
    while(word != NULL)
    {
        ++counter;
        word = strtok(NULL, " \t\n\v\f\r");
    }
    return counter;
}

bool isNumber(char* str)
{
    int i, len = strlen(str);
    for (i=0; i<len; ++i)
        if (!isdigit(str[i]))
            return false;
    return true;
}

Upvotes: 0

P.W
P.W

Reputation: 26800

If I understand your question right, you want to print an error when the input is anything other than an integer and this includes newline as well. You can do that using a char array and the %[] specifier.

Example:

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

int main(void)
{
    int patients;
    char str[10];
    printf("Enter number of patients:");
    int numberOfInputs = scanf("%[0-9]", str);

    if (numberOfInputs != 1) {
      printf("ERROR: Wrong number of arguments. Please enter one argument.\n");
    }
    patients = atoi(str); //This is needed to convert the `str` back to an integer      
}

This will print the error when the user just hits ENTER as well.

Upvotes: 1

Related Questions