Belphegor
Belphegor

Reputation: 1843

How should I make it so that if the user inputs something I don't want, it will trigger a message in C?

I am new to C programming and I currently have this simple code that asks the user to enter a floating number, and with that value it would print it out, but I want to know how to make it so that it gives a message if user inputs anything, but a number. I know you need to use an if and else statement, but not sure what to use for the argument part..

My code so far(not done code yet until I find out how to handle inputs of non number):

#include <stdio.h>

int main()
{
    float input;
    printf("Hello please enter a float number!\n";
    scanf( "%f", &input);
}

I know you need to use an if and else statement, but I come from a Python background so the syntax is completely different... Looking for some guidance as to how to handle this! Nothing too advanced please, I am looking for something simple.

Edit:

Thank you all of you guys for great replys! It really help me understand more of the different ways to handle such problems in future! :D

Upvotes: 1

Views: 160

Answers (2)

To check that the input did input some number like 1.2 (and not something else like !acd) you should use the return value of scanf (it returns the number of scanned items).

Read the documentation of scanf(3) (and in general, read the documentation of every function that you are using):

These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in 
the event of an early matching failure.
 The value EOF is returned if the end of input is reached before
 either the first successful conversion or a matching failure occurs.

Concretely you might code something like:

int main() {
   float input=0; // or perhaps NAN
   printf("Hello please enter a float number!\n";
   if (scanf( "%f", &input)<1) {
     perror("bad input"); exit (EXIT_FAILURE);
   }
}

Of course, you might want to repeat the question till you get a floating point number. Then use a while. And you might handle differently invalid input (scanf returning 0) with end-of-file or error-on-read condition (scanf returning EOF), maybe:

int main() {
  float input=0;
  int nbscan= -1;
  do {
    printf("Enter a number:\n");
    nbscan = scanf("%f", &input);
    if (nbscan==EOF) { perror("EOF from scanf"; exit(EXIT_FAILURE); }
  } while (nbscan<1);
  return 0;
 }

However, this usually means that the user did type some input; perhaps you want to catch the case when the user did not type anything within 500 milliseconds. Then, you should operating system specific functions or syscalls. On Linux and Posix, the poll(2) syscall is relevant.

Upvotes: 1

Dolda2000
Dolda2000

Reputation: 25855

While doukremt is right in that you can check the return value of scanf to detect this particular error, scanf is really just for quick-and-dirty solutions, and has many failings when you want more robust behavior.

I would instead recommend reading a whole line of input as a string (using, for instance, fgets), and then using strtof or some similar function to convert it, which would tell you using its third argument when the conversion failed. Then you could check explicitly for every possible error condition and handle them appropriately.

Upvotes: 3

Related Questions