1

Ok so I want to preface this by saying I know very little about C. I'm studying it independently at my high school and so I've come to you folks for help. I'm taking C1 and I don't know if that's # or ++ or just C or even what the differences are between those. Anyways my question is how do I make sure that a user enters an integer instead of a float, or make sure that the person did not enter a & symbol into the float input.

I code in notepad and save them as .c files and then gcc them in a command prompt if that will give you any idea about what I'm working with.

ChapmIndustries
  • 1,401
  • 4
  • 17
  • 19
  • 3
    wait - how do you write code without knowing which language it's written in? – Nim Sep 18 '12 at 15:31
  • @Nim There are lessons that go along with what I'm learning and they just tell me what I have to make and I know a few statements to get all of that done. I know the code I just don't know what it is called. It's not very hard to program in a language without knowing what the hell you're doing :) Think about all of the little kids that know how to program. – ChapmIndustries Sep 18 '12 at 15:45

3 Answers3

0

You can use the functions declared in the header file ctype.h, in particular the function isdigit.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Alfred Angkasa
  • 1,381
  • 3
  • 17
  • 35
0

Well, I suppose you are programming C, by your comment about gcc. But going into the question, if you are using scanf, then it pretty much checks everything, you just have to know how to use it, for example:

int main(void)
{
    float input;
    if(scanf("%f", &input) == 1)
    {
        printf("OK, I could read your float: %f.\n", input);
    }
    else
    {
        printf("Not OK, I couldn't read your float.\n");
    }
    return 0;
}

If I input 5.4654, it will print the correct number. If I input 5.4&654 it will print 5.4, because there was a float in there, and it went until the & character. But if I input abc, it will fail, and print it could not read your float.

The reason this works is because scanf returns the number of successful data read. So if you ask for "%f %f %d", you have to check if the return value is 3, if so, everything went OK, despite random characters that would be in the way but not being read.

Or alternatively, talking about integer types (and similar), you could use the functions provided by ctype.h.

Toribio
  • 3,963
  • 3
  • 34
  • 48
  • I'd suggest `fgets` instead of `scanf`. Do something like this `char buff[20]; fgets(buff, size, stdin);`. It seems that `scanf` could break your propram with a buffer overflow. I think that you're safer with `fgets` since you can size up your input. – Chris Sep 18 '12 at 17:23
  • For catching strings, pretty much yes. – Toribio Sep 18 '12 at 17:32
0

Bestest way is to read lines of input with fgets() and parse the lines. For numeric input, strtol() for integers and/or strtod() for floating-point numbers are the most comprehensive function to use.

eg

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

int main(void) {
    char line[100];
    char *err;
    int intval;

    printf("Enter a number:\n");
    fgets(line, sizeof line, stdin);

    errno = 0;
    intval = strtol(line, &err, 10);
    /* check for errors */
    if (errno) /* error */;
    if (*err != '\n') /* error */;

    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198