Hey folks, wrote this program to check if a string is an integer. It checks for + or - sign at the front of it, but it spat out some errors.
I think I broke it.
Here is the code:
Code:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int getInteger(char*);
int main(void) {
char str[99];
int x;
printf("Enter an integer: ");
gets(str);
char *moo = str;
if (getInteger(str) == 1) {
printf("The number you entered is not a valid integer.");
}
else {
x = atoi(str);
printf("the number you entered is %d", x);
}
return 0;
}
int getInteger(char *moo) {
if (*moo == '-' || *moo == '+') {
++moo;
printf("Positive or negative sign detected.\n");
}
if (*moo == NULL) {
return 1;
printf("You messed up.\n");
}
while (*moo) {
if (!isdigit(*moo)) {
printf("You are not a digit!\n");
return 1;
}
else {
++moo;
}
}
return 0;
}
Here are the warnings I get:
warning: unused variable 'moo'
in function 'getInteger'
warning: comparison between pointer and integer
Code:
if (*moo == NULL) {
Can you guys help me fix these errors?
TIA!