How do I only allow numbers

• 11-30-2010
A-myth
How do I only allow numbers
Hello, I just need the function that only allows numbers to be used for a random numbers game.
Here is the code

Code:

```#include <stdio.h> startGuessing() {         //this function is where the guessing game launches from         int i=0,r=0, y=1;         r=rand(time(0))%100+1;     printf("The Number is between 1 and 100\n");         do         {         printf("What is your guess?:");         scanf("%d",&i);            if(i>100)             {         printf("You have entered an invalid number. The Number is between 1 and 100.\n");         continue;         }     else if(i<1)         {         printf("You have entered an invalid number. The Number is between 1 and 100.\n");         continue;         }         else if(i>r)                 {                 printf("You are too high. Try again.\n");                 continue;                 }         else if(i<r)                 {                 printf("You are too low. Try again.\n");                 continue;                 }         else                 {                 printf("You are correct!\n");                 break;                 }         y++;         }         while(y<100); return 0; } startDice() {         //this function is where the dice game launches from         printf("You have chosen to play 5 ROLLS! The object of the game is to roll 3 even numbers with only 5 rolls. Lets Start!\n\n");                 int a=0, b=0, c=0, d=0, e=0, f=0;             a=rand(time(0))%6+1, b=rand(time(0))%6+1, c=rand(time(0))%6+1, d=rand(time(0))%6+1, e=rand(time(0))%6+1;         printf("To roll, "); system("PAUSE");     printf("You rolled %d for roll one.\n\n",a);     printf("To roll again, "); system("PAUSE");     printf("You rolled %d for roll two.\n\n",b);     printf("To roll again, "); system("PAUSE");     printf("You rolled %d for roll three.\n\n",c);     printf("To roll again, "); system("PAUSE");     printf("You rolled %d for roll four.\n\n",d);     printf("To roll again, "); system("PAUSE");     printf("You rolled %d for roll five.\n\n",e);         int rA=0, rB=0, rC=0, rD=0, rE=0;         rA=a%2;     rB=b%2;     rC=c%2;     rD=d%2;     rE=e%2;         f=rA+rB+rC+rD+rE;         if(f<3)     {     printf("You won!!!\n\n");     }     else     {     printf("You lose.\n\n");     } } printMenu() {         int input=0;         do {                 //print the menu                 printf("Please select your game: \n\n");                 printf("\t1: Guessing Game\n");                 printf("\t2: Dice Game\n");                 printf("\t-1: Exit Program\n");                                 //take the user's input                 scanf("%d",&input);                                 //check to see which game the user wants                 if (input==1) {                         startGuessing();                 }                 else if(input==2)                 {                         startDice();                 }                                 //if the user enters -1, exit the loop (and thus the program)         } while (input!=-1); } int main (void) {         //this seeds the system's time into the random generator         srand(time(0));         printf("\nA M A Z I N G  G A M E    v1.0\n");                 //load the main menu         printMenu(); }```
• 11-30-2010
claudiu
First of all don't forget to return 0 from main. Compile your code with all the warnings enabled.

In terms of making sure you only read in numbers you basically have to read the input as a string and then use strtol() to check that the input is a valid number. After that continue to check if it is in the range you want it.
• 11-30-2010
cas
I assume you want the user to be able to type in a value, but you want to make sure it's numeric only.

The general way this is achieved is to use fgets() to read a line and convert it with strtol(). strtol() can help you figure out if the user typed something invalid, in which case you can ask again.