escape routine and array elements

FrawgLips
FrawgLips used Ask the Experts™
on
/* 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 "you win" being displayed.  This is wrong.  How to rewrite it so that in order to
win: the "v" number must be the same as the "win" number, the "w" number must be the same as the "place" number and the "x" number must be the same as the "show" number.  
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?  I have tried comparisons like "if ( (v-1) ==(numbers[0])&& (w-1 == (numbers[1])&&
(x-1)==(numbers[2]) ) printf("\n you win");  I don't understand what is happening here.  
  */

#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));
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hmm use rand() between 1 and 3 to decide who wins and then again to see who i second and then for third?

much easier to produce a random winner
Commented:
The scanf function is somewhat crude w.r.t. handling general input. I would expect that the line:

scanf("%i",&v);

is either going to put a number in v, or, if it got bad input (like some characters, or nothing) put nothing in v. The latter case should be detectable by inspecting the value that scanf returns. It is supposed to return the number of fields successfully converted and assigned, and should return a 0 if no fields were assigned.

The isdigit macro/function, by the way, only works in a meaningful way on character data. There would be no way to use it with this scanf statement.

As for determining whether the user has picked the horses in the correct order, I think a simple strategy would be to annotate the order in the numbers array. At present numbers elements are all set to 1 if they are picked. If instead, you set the first one to 1, the second to 2 and the third to 3 (which you could do by assigning i+1 to numbers[b]) you would have the ordered noted.

Then line 132 would presumably become:

a0=(numbers[v-1]==1) && (numbers[w-1]==2) && (numbers[x-1]==3);/* 1st,2nd,3rd vwx*/

and a0 would still designate whether the user won or not.

         
>>     printf("\n\nEnter the number you think will win  ");
>>         scanf("%i",&v);
>> 
>> /*76*/   if ( v>4 || v==0 )
>>         printf("\nThat is not an option!");

Just say,

    char number[2];

    printf("\n\nEnter the number you think will win  ");
    scanf("%s",number);

    v = atoi( number );

    if ( v>4 || v==0 )
        printf("\nThat is not an option!");

/*132*/     a0=(numbers[v-1]==1) && (numbers[w-1]==1) && (numbers[x-1]==1);/* 1st,2nd,3rd vwx*/

Just nest the if statements in the order you want the horses to win.

For example:

if( numbers[v-1] == 1 )
{
    if( numbers[w-1] == 1)
    {
        if( numbers[x-1] == 1 )
            a0 = 1;
    }
}





Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Scratch that, I does not work.
I agree with imladris on regard to line 132.

Author

Commented:
Imladris & Exceter,

I have used both of your answers so how about a split on the points?  If that is not agreeable let me know.
The program works the way I want and I now understand how it works...(well, most of it).
Thanks to both of you

Commented:
Splitting would be fine.

Commented:
splitting is never fine. Give all the points to me instead.
Splitting is fine with me.

Commented:
:) I will stop being so childish.
FrawgLips

You wanted to award " Imladris 150 and Exceter 100" points.

I have reduced the points on this question from 200 to 150 as indicated by your request at Community Support. Please copy the URL and create a new question in this topic area for the other Experts to whom you wish to award points. The title of the question should read "Points for", followed by the Expert's name. In the question itself, you should paste the link to the original question and perhaps a comment stating that the points are for their help with that question. Once you have created the new questions, you can go back to the original, and accept the comment from the Expert for whom you did not create a new question. The Experts will  comment in your new "Points for" question(s), which you then accept and grade to close.
If you have any questions, or would prefer that a Moderator perform these actions for you, please don't hesitate to ask.
Thank you.

** Mindphaser - Community Support Moderator **

Author

Commented:
The split is this way because you did in fact have the answer for my first part of the question..Exceter had the code that worked.
Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial