1
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!

annie
  • 23
  • 1
  • 4
  • Google for "argc argv" – Sandy Sep 28 '18 at 05:08
  • What does not work for this code? Can you give an example? – Rishikesh Raje Sep 28 '18 at 05:10
  • Read about scanf() here - https://linux.die.net/man/3/scanf . Check the return type of scanf() section to understand your code behavior. – MayurK Sep 28 '18 at 05:26
  • Do I read correctly, that you actually want to read a line of user input, then if it contains more than one valid number, give error message? In that case you need to read the line first, then parse it. – hyde Sep 28 '18 at 06:48

2 Answers2

1

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.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • This is for my understanding. How can I enter two integer inputs? I tried this int numberOfInputs = scanf("%[0-9]d %[0-9]d", &patients, &apatients2); It returns numberOfInputs =1. – MayurK Sep 28 '18 at 05:54
  • For multiple values, you have to use a char array with sscanf and then convert them accordingly. – P.W Sep 28 '18 at 06:19
  • Thank you, this works perfectly for the new line. It does not seem to hold if a user inputs more than one argument but I will work off of what you have posted here. Thanks again! – annie Sep 28 '18 at 14:47
  • You're​ welcome. I have answered what is to be done in case of multiple inputs in one of the above comments. You can accept this answer if it solves the problem in the question. – P.W Sep 28 '18 at 15:10
0

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;
}
Zoltán
  • 678
  • 4
  • 15