Don't like ads? PRO users don't see any ads ;-)
Guest

Random Number Andrew Page

By: infestor1 on May 2nd, 2012  |  syntax: C  |  size: 3.13 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. int random(); // Function prototypes
  8. int input(char []);
  9. void result(int);
  10. void n();
  11.  
  12. int main() {
  13.  
  14.        int j, in, c = 0, r = 0; // Declare variables. R = result. C = continue variable. IN = input number. IND = input string.
  15.        char ind[10];
  16.  
  17.        do { // Do while loop to repeat
  18.  
  19.                j = random(); // Random number
  20.  
  21.                printf("The random number is: %i\n", j);
  22.                //Above code used for testing
  23.  
  24.                in = input(ind); // Recieve user input
  25.                n();
  26.  
  27.                if(in == -1)  // Stop the game
  28.                {
  29.  
  30.                        printf("Thanks for playing!");
  31.                        c = 1;
  32.                        r = 10;
  33.  
  34.                }
  35.                else { // Compare user input to random number
  36.  
  37.                        if(isdigit(ind[0])){
  38.                                if(in == j)
  39.                                        r = 1; // Equals
  40.                                else if(in >= j)
  41.                                        r = 2; // Too high
  42.                                else if(in <= j)
  43.                                        r = 3; // Too low!
  44.                                else
  45.                                        r = 4;
  46.  
  47.                                result(r);
  48.                        }
  49.                        else {
  50.  
  51.                                printf("Please enter a number.");
  52.                                n();
  53.                                n();
  54.                        }
  55.                }
  56.  
  57.                 // Generate result
  58.  
  59.        } while (c == 0); // Repeat!
  60.  
  61.        return 0;
  62. }
  63.  
  64. int random(){ // Random number generator
  65.  
  66.        int x;
  67.        srand(time(0)); // Seed the randomizer
  68.        x = rand() % 1001; // Generate random num 1-1000
  69.  
  70.        return x; // Return number to program
  71. }
  72.  
  73. int input(char input[]){ // Get user input
  74.  
  75.        int i;
  76.  
  77.        printf("Type in the number to guess (1-1000): ");
  78.        gets(input); // Get user input
  79.        i = atoi(input);
  80.  
  81.        return i;
  82. }
  83.  
  84. void result(int r){ // Return result
  85.  
  86.        switch(r){
  87.  
  88.                case 0:
  89.                        printf("Error!"); // Error, just for kicks.
  90.                        n();
  91.                        n();
  92.                break;
  93.                case 1:
  94.                        printf("Correct!"); // Number == rand
  95.                        n();
  96.                        n();
  97.                break;
  98.                case 2:
  99.                        printf("Too high!"); // Number is too big
  100.                        n();
  101.                        n();
  102.                break;
  103.                case 3:
  104.                        printf("Too low!"); // Number is too small.
  105.                        n();
  106.                        n();
  107.  
  108.                break;
  109.                case 4:
  110.                        printf("Error!"); // Error, just for kicks.
  111.                        n();
  112.                        n();
  113.                break;
  114.                case 10:
  115.                break;
  116.  
  117.        }
  118.  
  119. }
  120.  
  121. void n() //Newline
  122. {
  123.  
  124.        puts("");
  125.  
  126. }