Thread: This odd program works? Reading string and checking if integer. Need warning explaind

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    8

    This odd program works? Reading string and checking if integer. Need warning explaind

    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'
    Code:
    char *moo = str;
    in function 'getInteger'
    warning: comparison between pointer and integer
    Code:
    if (*moo == NULL) {
    Can you guys help me fix these errors?

    TIA!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    1; remove that line
    Code:
    char *moo = str;
    moo is never used

    2: NULL is defined as a pointer type. You want to compare to the 0 character -> use '\0'
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Depending on compiler settings, you will also get warnings because some of the printf() statements are immediately after a return statement. They can therefore never be executed.

    And don't use gets(). Use fgets() instead.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2013, 09:19 PM
  2. Replies: 14
    Last Post: 01-09-2013, 06:35 AM
  3. pointer from integer without a cast - warning
    By Andreea Coman in forum C Programming
    Replies: 17
    Last Post: 01-11-2012, 02:48 PM
  4. Reading integer from string
    By dr_kaufman in forum C Programming
    Replies: 2
    Last Post: 04-30-2011, 09:40 AM
  5. [Warning] comparison between pointer and integer
    By rkooij in forum C Programming
    Replies: 5
    Last Post: 05-12-2006, 08:43 AM