C problem (probably simple...)

Associate
Joined
12 Jun 2003
Posts
898
Location
Wicklow, Ireland
how's it going, i'm learning a bit of C at the moment and i'm having problems.

here's my story so far, i've been given an assignment to do (basically to program a fully functional lottery).

so i have the user selecting from a menu to scan in 7 numbers, but my problem is how can i keep the user from selecting int's higher than 42 (42 is the highest in the irish lotto)?

i'm not looking for the code as i'd prefer to learn it myself but any pointers would greatly be appreciated.
 
You could just validate the input with a simple if statement?

To prevent the user selecting the same number twice, just store the numbers and check the input each time the it is submitted?

That's how I'd do it anyway :)
 
dave_a141 said:
You could just validate the input with a simple if statement?

right, i thought it would be like an if statement.

so i have the user scanning their numbers into an array (i have to use pointer notation aswell in the assignment).

Code:
#include <stdio.h>
#include <stdlib.h>

#define LOTTO_SIZE 7
main()
{
      void welcome(void) ; 
      void menu1(void) ;
      int sortarray(int *) ;
      
      int chosen_numbers[LOTTO_SIZE] ;
      int *pointer ; 
      int i ; 
      int sortedarray[LOTTO_SIZE] ; 
      
      pointer = chosen_numbers ; 
      
      welcome() ; 
      menu1() ;
              for (i = 0 ; i < LOTTO_SIZE ; i++)
              {
                  scanf("%d", pointer+i) ;
              }
              for (i = 0 ; i < LOTTO_SIZE ; i++)
              {
                  printf("%d ", *(pointer+i)) ;

} //end main

void welcome(void)
{
     printf("Welcome to the lottery!\n\n") ; 
}
     
void menu1(void)
{
     printf("Please select 7 numbers!\n") ; 
}

this is what i've written so far so should it go something like this where i'm scanning into the array?

Code:
[b]if(*pointer < 42)[/b]
{
for (i = 0 ; i < LOTTO_SIZE ; i++)
              {
                  scanf("%d", pointer+i) ;
              }
              for (i = 0 ; i < LOTTO_SIZE ; i++)
              {
                  printf("%d ", *(pointer+i)) ;
              }
}
else
{
       printf("try again\n") ; 
}
 
Id run the scanf call with a (temporary) local variable.

Also, instead of using a for loop i'd use a while loop. Something like:

Code:
while (numbers_entered < 7)
{
    int number = GetNumber() ;
    if (ValidNumber (number)
   {
      //Add number to array
      // increment numbers_enetered
   }
   else
   {
      // Tell the user that the entry isnt valid
   }
}
 
Back
Top Bottom