Thread: How do I only allow numbers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    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();
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

Tags for this Thread