1

I'm trying to write a simple code to check if a string only has numbers in it. So far it's not working, any help would be appreciated.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char numbers[10];
    int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i <= numbers ; ++i)
    {
        if(isalpha(numbers[i]))
        {
            correctNum = 1;
            break;
        }
    }

    if(correctNum == 1)
    {
        printf("That number has a char in it. FIX IT.\n");
    }
    else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
}
SycoSins
  • 39
  • 1
  • 2
  • 8
  • 2
    Define "not working"? You're not checking if everything is numeric, for starters. You're checking if there's a letter in it. You probably would rather want to use `isdigit()` – Sami Kuhmonen Jan 21 '17 at 07:49
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. – Joe C Jan 21 '17 at 08:13
  • 1
    Use a *positive* test with `isdigit` as you may encounter chars that are not alpha and not digits... – Jean-Baptiste Yunès Jan 21 '17 at 08:19
  • `char numbers[10];` is fairly small, suggest 1) `char numbers[100];` 2) `isdigit()` and 3) `scanf("%99s", numbers);` – chux - Reinstate Monica Jan 21 '17 at 09:11
  • @JoeC This is just a very small part of a significantly larger project I'm working on. I was never taught any of this in a school setting, I'm just trying to implement some extra nifty features. – SycoSins Jan 21 '17 at 09:48
  • `i <= numbers` is an error and your compiler should have diagnosed it. Pay attention to compiler messages. – M.M Jan 21 '17 at 10:37

5 Answers5

6

Adding to the others answers, you can also use strtol to determine if a string has all numbers or not. It basically converts the string to an integer, and leaves out any non-integers. You can read the man page for more information on this function, and the extensive error checking you can do with it.

Also, you should use:

scanf("%9s", numbers);

Instead of:

scanf("%s", numbers);

To avoid buffer overflow.

Here is some example code:

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

#define MAXNUM 10
#define BASE 10

int main(void) {
    char numbers[MAXNUM];
    char *endptr;
    int number;

    printf("Enter string: ");
    scanf("%9s", numbers);

    number = strtol(numbers, &endptr, BASE);

    if (*endptr != '\0' || endptr == numbers) {
        printf("'%s' contains non-numbers\n", numbers);
    } else {
        printf("'%s' gives %d, which has all numbers\n", numbers, number);
    }

    return 0;
}

Example input 1:

Enter string: 1234

Output:

'1234' gives 1234, which has all numbers

Example input 2:

Enter string: 1234hello

Output:

'1234hello' contains non-numbers
RoadRunner
  • 25,803
  • 6
  • 42
  • 75
1

You might consider using strspn:

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

int main(int argc, char* argv[]) {
    int i;
    for (i=1; i < argc; i++) {
        printf("%s %s\n",
            strlen(argv[i]) == strspn(argv[i], "0123456789") ? "digits" : "mixed",
            argv[i]
        );
    }
}

Demoed:

$ ./try foo 123 ba23a 123.4
mixed foo
digits 123
mixed ba23a
mixed 123.4

strspn returns the initial number of characters from the first argument that appear in the second argument. Super simple examples:

strspn("abba", "a");  // == 1
strspn("abba", "b");  // == 0
strspn("abba", "ab"); // == 2
bishop
  • 37,830
  • 11
  • 104
  • 139
0
for(i = 0 ; i <= numbers ; ++i) //how is this supposed to work.

Run the loop from 0 to 1 less than the length of the input.

for(i = 0 ; i < strlen(numbers) ; ++i)
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34
0
#include <stdio.h>
#include <string.h>

int main()
{
    char numbers[10];
    int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i < 10 ; i++)
    {
        if(numbers[i]<48||numbers[i]>57)
        {
            correctNum = 1;
            break;
        }
    }

    if(correctNum == 1)
    {
        printf("That number has a char in it. FIX IT.\n");
    }
    else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
}
-1

You have an error in the for loop - for(i = 0 ; i <= numbers ; ++i)

numbers is pointer and the comparison of it with integer is forbidden. Correct Code -

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
   char numbers[10];
   int i, correctNum = 0;

    scanf("%s", numbers);

    for(i = 0 ; i < strlen(numbers) ; ++i)
    {
       if(!(numbers[i]>='0' && numbers[i]<='9'))
       {
           correctNum = 1;
            break;
       }
    }

    if(correctNum == 1)
    {
         printf("That number has a char in it. FIX IT.\n");
     }
   else
    {
        printf("All numbers. Good.\n");
    }
    return 0;
 }
GAURANG VYAS
  • 689
  • 5
  • 16