5 Replies - 534 Views - Last Post: 22 August 2014 - 05:37 AM Rate Topic: -----

#1 djsa4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-July 14

Code for guessing game

Posted 02 August 2014 - 12:47 PM

Hello, I just want to make sure that my code looks alright before I submit it to my professor.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()
 
{
    int  iRandomNum;
    char  iResponse;
    srand(1-10);
     
    iRandomNum = (rand()%10)+1;
 
    printf("\nGuess a number between 1 and 10: ");
    scanf("%c", &iResponse);
 
    if (!isdigit(iResponse)) {
        printf("You didn't enter a number\n\n");
        return 0;
    }
             
    if ( (iResponse - '0')== iRandomNum) {
        printf("\nYou guessed correctly\n");   
        }  
        else {
        printf("\nSorry, you guessed incoorectly\n");
        printf("The correct guess was %d\n", iRandomNum);
    }
}


Any tips would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Code for guessing game

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Code for guessing game

Posted 02 August 2014 - 12:51 PM

Did you test it with the number 10? You should print out what the user guessed too.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Code for guessing game

Posted 02 August 2014 - 12:54 PM

What was your intent with seeding the random number generator on line #9? It looks like you are always seeding it with the value of -9.
Was This Post Helpful? 0
  • +
  • -

#4 pythonuser007  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 26
  • Joined: 25-January 14

Re: Code for guessing game

Posted 22 August 2014 - 01:23 AM

View Postdjsa4, on 02 August 2014 - 12:47 PM, said:

Hello, I just want to make sure that my code looks alright before I submit it to my professor.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main()
 
{
    int  iRandomNum;
    char  iResponse;
    srand(1-10);
     
    iRandomNum = (rand()%10)+1;
 
    printf("\nGuess a number between 1 and 10: ");
    scanf("%c", &iResponse);
 
    if (!isdigit(iResponse)) {
        printf("You didn't enter a number\n\n");
        return 0;
    }
             
    if ( (iResponse - '0')== iRandomNum) {
        printf("\nYou guessed correctly\n");   
        }  
        else {
        printf("\nSorry, you guessed incoorectly\n");
        printf("The correct guess was %d\n", iRandomNum);
    }
}


Any tips would be greatly appreciated.


Here is a tip, actually a whole program.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void play_game();
void initialise_game(int *ptrhigh, int *ptrlow, int *ptrcount, int *ptrnumber);
void get_human_input(int *ptrguess, int *ptrcount, int *ptrlow, int *ptrhigh);
void check_guess(int *ptrguess, int *ptrnumber, int *ptrhigh, int *ptrlow, int *ptrgame_won, int *ptrcount);
void clear_input_buffer(char ch);

main()
{
 char ch;
 int play_again = 1;
 while (play_again)
 {
  play_game();
  printf("\nDo you want to play again? <Y/N> " );
  ch = getchar();
  while ((ch != 'Y' && ch != 'y') && (ch != 'N' && ch != 'n'))
  {
   printf("\nDo you want to play again? <Y/N> " );
   ch = getchar();
   clear_input_buffer(ch);
   ch = getchar();
  }
  if (ch == 'N' || ch == 'n')
  {
   play_again = 0;
  }
  else
  {
   clear_input_buffer(ch);
  }
 }
}

void clear_input_buffer(char ch)
{
 while (ch != '\n')
 {
  ch = getchar();
 }
}

void play_game()
{
 /*Initialise pointer variables and allocate memory for them.*/
 int *ptrhigh = malloc(1 * sizeof(int)); 
 int *ptrlow = malloc(1 * sizeof(int)); 
 int *ptrguess = malloc(1 * sizeof(int));
 int *ptrcount = malloc(1 * sizeof(int));
 int *ptrnumber = malloc(1 * sizeof(int));
 int *ptrgame_won = malloc(1 * sizeof(int));
 initialise_game(ptrhigh, ptrlow, ptrcount, ptrnumber);
 /*Loop while game has not been won.*/
 while (*ptrgame_won == 0)
 {
 get_human_input(ptrguess, ptrcount, ptrlow, ptrhigh);
 check_guess(ptrguess, ptrnumber, ptrhigh, ptrlow, ptrgame_won, ptrcount);
 }
 /*When game won then show a message based on how many guesses it took to win.*/
 printf("\033[22;36m");
 if (*ptrcount < 7)
   printf("\nWell done you guessed the number %d in only %d guesses. \n", *ptrnumber, *ptrcount);
 else if (*ptrcount > 6 && *ptrcount < 10)
   printf("\nWell done you guessed the number %d in %d guesses. \n", *ptrnumber, *ptrcount);
 else
   printf("\nFar out you guessed the number %d in a massive %d guesses. \n", *ptrnumber, *ptrcount);
 /*Change console print colour back to default.*/
 printf("\033[22;32m");
 /*Free all the pointer variables.*/
 free(ptrhigh);
 free(ptrlow);
 free(ptrguess);
 free(ptrcount);
 free(ptrnumber);
 free(ptrgame_won);
}


void check_guess(int *ptrguess, int *ptrnumber, int *ptrhigh, int *ptrlow, int *ptrgame_won, int *ptrcount)
{
 if (*ptrguess >= *ptrhigh || *ptrguess <= *ptrlow)
 /*Check if guess is outside of the guess range.*/
 {
  printf("\033[22;31m");
  printf("\nYour guess needs to be from %d to %d.\n", *ptrlow, *ptrhigh);
  printf("\033[22;32m");
  *ptrcount = *ptrcount - 1;
 }
 else if (*ptrguess > *ptrnumber)
 /*If guess is too high make the guess the highest number.*/
 {
  *ptrhigh = *ptrguess;
 }
 else if (*ptrguess < *ptrnumber)
 /*If guess is too low make guess the lowest number*/
 {
  *ptrlow = *ptrguess;
 }
 else if (*ptrguess == *ptrnumber)
 /*If guess equals number then game has been won.*/
 {
  *ptrgame_won = 1;
 }
}

void get_human_input(int *ptrguess, int *ptrcount, int *ptrlow, int *ptrhigh)
{
 char ch, entry[5]; 
 int count = 0, loop, flag = 1;
 printf("\nEnter your guess between %d and %d: ", *ptrlow, *ptrhigh);
 /*Read the input buffer.*/
 ch = getchar();
 while (ch != '\n' && count < 4)
 {
  entry[count++] = ch;
  ch = getchar();
 }
 entry[count] = '\0';
 /*Clear the input buffer if there is still any input left.*/
 while (ch != '\n')
 {
  ch = getchar();
 }
 /*Check to make sure input was an integer*/
 for (loop = 0; loop < count; loop++)
 {
  if (entry[loop] < 48 || entry[loop] > 57)
   flag = 0;
 }
 /*Convert from a character to a number if input is valid.*/
 if (flag == 1)
   *ptrguess = atoi(entry);
 else
   *ptrguess = 0;
 *ptrcount += 1;
}


void initialise_game(int *high, int *low, int *count, int *number)
{
 srand(time(NULL));
 *low = 1;
 *high = 100;
 *count = 0;
 *number = 1;
 /*Check to make sure random number is between 1 and 100 and not either of those numbers.*/
 while (*number == 1 || *number == 100)
 {
  *number = rand() % 99 + 1;
 }
}


This post has been edited by pythonuser007: 22 August 2014 - 01:24 AM

Was This Post Helpful? -3
  • +
  • -

#5 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 233
  • View blog
  • Posts: 842
  • Joined: 18-April 09

Re: Code for guessing game

Posted 22 August 2014 - 01:33 AM

Quote

Here is a tip, actually a whole program.


Well, looking at the OP's code, I think he has not yet covered topics such as pointers and functions. It's better to make it simple for now.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: Code for guessing game

Posted 22 August 2014 - 05:37 AM

pythonuser007: Just giving people code doesn't help them learn. We try to guide them into discovering the issues with their code, or direct them to a different path if they are completely lost in the weeds. Just saying: "here, this solves your problem" without any attempt to explain doesn't aid in their education.

To make matters worse, the code you posted allocates memory for things which don't need allocation, and has a lot of tight coupling.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1