0
#include <stdio.h>
#include <string.h>
#include <math.h>

    void check(char *s){
        char str[24] = s;
        int i;
        fgets(str, sizeof(str), stdin);
        boolean valid = TRUE;
        for (i = 0; i < strlen(str); ++i){
            if (!isdigit(str[i])){
            printf("NOT a valid Integer");
            valid = FALSE;

            break;
            }
        }
        printf("An integer!");
    }

    int main()
    {
        int userInput;
        char str;

        scanf("%d%c", &userInput, &str);
        check(str);
        .
        .
        .


        return 0;
    }

I keep getting error in my code, I'm trying to find a way to use a separate check function to check if the user input a valid integer, i.e.: numbers like 1,345, 7899,...are good; while typing 'a23','@3','12', '24.4',...will halt the program. Any ideas will help, thanks. C Language.

xpluffy
  • 79
  • 1
  • 2
  • 9
  • 1
    `char str[24] = s;` doesn't do what you think it does. Not sure what you are trying to do to be honest. – Alok Save Sep 25 '14 at 03:54
  • @AlokSave To clarify the wording, I'm still new to C and trying to find a way to use my check() function to check if the user inputs a valid integer. If the user say typed 'a2', scant will read it and and the check function will halt the program because it isn't a valid integer. – xpluffy Sep 25 '14 at 03:59
  • possible duplicate of [Check if input is integer type in C](http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c) – 999k Sep 25 '14 at 04:22
  • @999k Yeah, I did base my checking function base on that...But I need help why is the code not working...I been trying to work on the syntax etc...But my goal is to use a check() function to check if the user input via scant is a Integer – xpluffy Sep 25 '14 at 04:30
  • 1
    There's just so much wrong, here. If the user enters `a2`, then your `scanf()` call will just fail, and there'll be no input for you to check. If the user enters an integer, then by definition the first character after that will be read by `%c`, and will by definition by a non-integer character. Either way, anything you pass to `check()`, you immediately overwrite with your `fgets()` call. – Crowman Sep 25 '14 at 04:50
  • Your code won't compile,you missed some header files – Spikatrix Sep 25 '14 at 05:00

1 Answers1

-1

change your code like this in main function

char str[24];
scanf("%d%s", &userInput, str);
Kumar
  • 3,782
  • 4
  • 39
  • 87
  • Still didn't work, for I even check if str is even getting pass in the function by changing check() to like this: void checkInt(char *s){ printf("FLAG%s",s); }///And it's printing (NULL) – xpluffy Sep 25 '14 at 04:26
  • check to print the input value in the main function itself. if its not NULL then surely it will pass in the function. – Kumar Sep 25 '14 at 04:40
  • 1
    Passing an uninitialized pointer to `scanf()` is not going to result in happiness. – Crowman Sep 25 '14 at 04:57