# 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");
}
}

{
int input=0;
do {
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");