Thread: escape & array elements

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    escape & array elements

    /* Horse race simulation

    The program runs as is but:
    line 76 needs to find if anything other than a number was entered.
    then add to line 81. If a letter is entered the program goes into
    an infinite loop (scrolls). use "isdigit" ?? if so, how?

    line 132. If the 3 selected horses show up IN ANY ORDER this line will result in
    "win" being displayed. This is wrong. How to rewrite it so that in order to
    win: the "v" number must come in first, the "w" number must come in second
    and the "x" number must come in third? Have tried and tried and tried to make
    this work right!
    Since all three drawn numbers will have a "1" in it's element how do you know
    which is which? How can you evaluate if the relationship is equal between v &
    win, w & place, x and show?*/
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <ctype.h>
    #include <conio.h>
    
    #define FIELD 4
    #define RESULTS 3
    #define DELAY 50000000
    #define TRUE 1
    #define FALSE !TRUE
    
    char play_again;
    int rnd(int range);
    int r;
    char* cNames[8]={{"Spendabuck"}, {"Cigar"}, {"War Admiral"},
    {"Sunday Silence"}, {"Snorts"}, {"Hoof n Mouth"},
    {"Santa Fe Sid"}, {"Seattle Slew"}};
    void srnd(void);
    
    int main()
    {
           int numbers[FIELD];
           int i,b,c,v,w,x,a0;
           long d;
           int index, index2;
           int iRandNames[4] ;   // array to be used for comparison
    
           srnd();
           printf("Welcome to Lucky Meadows\n");
           printf("\nThere are 4 horses in this race.");
           printf("\n\nPick the 3 finishers in the EXACT order they finish.");
           printf("\nFirst pick for WIN, 2nd pick for PLACE, 3rd pick for SHOW.\n");
    /*In case you don't know, WIN is for the winner of the race, PLACE is for the horse finishing 2nd and
    SHOW is the horse that finished in 3rd place.*/
    
            play_again=TRUE;
    
    	printf("\n\n\n        POST");/*put table heading to console*/
    
            for(index=0;index < 4;index++)
            {
             iRandNames[index] = rand()%8;
             for(index2 = index; index2 >= 0; index2--)
             if(iRandNames[index2] == iRandNames[index])
            {
    	  iRandNames[index] = rand()%8;/* equal, get a new name */
    	  index2=index;/* reset inner for() loop*/
            }
               printf("\n         %i.  %s", index+1, cNames[iRandNames[index]]);
            }
    
           while (play_again)
           {                          
           do
           {
    	 printf("\n\nEnter the number you think will win  ");
             scanf("%i",&v);
    
    /*76*/   if ( v>4 || v==0 )
             printf("\nThat is not an option!");
    
           }
    
    /*81*/ while ( v>4 || v==0 ) ;
    
           do
           {
    	  printf("\nEnter the number you think will finish 2nd  ");
              scanf("%i",&w);
    
    	  if ( w > 4 || w == 0 )
    	  printf("\nThat is not an option!");
    
    
              if (w==v)
    	  printf("\nYou already picked that number!\n");
            }
    
            while  ( (w>4) || (w==0) || (w==v) ) ;
    					 do
            {
    	  printf("\nEnter the number you think will finish 3rd  ");
    	  scanf("%i",&x);
    	  
              if (x>4 || x==0)
    	  printf("\nThat is not an option!");
    
    	  if (x==w || x==v)
    	  printf("\nYou already picked that number!\n");
    
            }
    
            while( (x>4) || (x==0) || (x==v) || (x==w) );
    
    	for (i=0;i<FIELD;i++)
    	numbers[i]=0;
    	printf("\nAnd They're Off!\n");
    
    	for(d=0;d<=DELAY;d++);
    	printf("\n    WIN        PLACE       SHOW\n");
    	for(i=0;i<RESULTS;i++)
    
    	{
    	for(d=0;d<=DELAY;d++);
    
    	do
    	{
            b=rnd(FIELD);
    	}
    	while(numbers[b]);
    	numbers[b]=1;
    	printf("     %i      ",b+1);
    	}
    
    /*132*/     a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/
    
    		if ( a0 )
    		printf("\n\nYou win!");
    
    		else
    		printf("\n\nYou lose!");
    
    		printf("\nPlay again? N for NO, any key for yes");
    		c=toupper(getch());
    		if(c=='N')
    	{
    	   play_again=!TRUE;
    	   printf("\n\nAdios");
    	}
    
         }
         return(0);
    }
    int rnd(int range)
    {
    int r;
    r=rand()%range;
    /*160*/ return(r);
    }
    void srnd(void)
    {
    srand((unsigned)time(NULL));
    }
    Thanks for any and all help

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    line 76 needs to find if anything other than a number was entered.
    then add to line 81. If a letter is entered the program goes into
    an infinite loop (scrolls). use "isdigit" ?? if so, how?
    Your variable then has to be a int right? I don't know exactly how, but if you enter anything else then a number, it will give an error.
    Instead of isdigit, try using
    Code:
    if(scanf("%d", var) == 0)
    I'm pretty sure that's correct...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM