# Thread: C guessing game (hmwrk)-It works as required & submitted: What would make it better?

1. ## C guessing game (hmwrk)-It works as required & submitted: What would make it better?

First. Thank you for being here and allowing me to make this post. As the title states this was a homework assignment. I've turned it in and it compiles and works. But, I'm wondering what methods would have been better for doing this assignment. I've no programming experience and I'm just trying to figure out the best way to do this sort of thing.

Thanks again!!

Code:
```//     Purpose: Program plays a guessing game.
//        Program randomly generates a number in the domain of 1-100
//        Program picks a number between 1 and 100.  User guesses the number.
//        The program provides feedback for each guess.
//        When you guess correctly user is given feedback about the number of guesses.
//
//    Inputs:
//        User enters a guess each round.
//
//    Outputs--
//        Ask for initial guess between 1-100
//        Provide feedback for each guess: ice cold, cold, cool, warm, hot, boiling hot
//        When correct guess is made: Provide number of turns to reach correct.

//These go before main()

#include <stdio.h>                             //Preprocessor Directive
#include <stdlib.h>                            //Preprocessor Directive
#include <time.h>                            //Preprocessor Directive

//main argument

int main() {                                //Function-main argument

//Code for random number generator-Instructor's code.//

srand(time(NULL));                       // Seed the random number generator

//variables

int goal = rand() % 100 + 1;             // goal will be 1-100, inclusive

int i = 1,                                // Index for guess counter
turn,                                // Guess counter for final answer.
guess,                                // User's guess.
distance;                            // Distance of guess from goal on the numberline.

printf("I have a number, 1-100.\n");

do    {

//printf("Super Secret # %d / Guess # %d\n",goal, turn);    //used for testing--if/ else if

printf("What's your guess?  ");

scanf("%d", &guess);                                    //User inputs integer

distance = abs(goal - guess);                                    //distance gets value.
turn = i++;                                                        //turn counter

printf("%d",guess);

if (guess < 1 || guess > 100)    {
printf(" is not 1-100.\n", guess);
}

else if (distance > 50)        {
printf(" is ice cold.\n");
}

else if (distance <= 50 && distance > 30)    {
printf(" is cold.\n");
}

else if (distance <= 30 && distance > 20)    {
printf(" is cool.\n");
}

else if (distance <= 20 && distance > 10)    {
printf(" is warm.\n");
}

else if (distance <= 10 && distance > 2) {
printf(" is hot.\n");
}

else if (distance <= 2 && distance >= 1) {
printf(" is boiling hot.\n");
}

continue;                                            //continue sends us back to the begining of do
}

while (guess != goal);                                            //while keeps us in loop until input is same as goal

printf(" is right in %d guesses.\n",turn);

return 0;                                        //e.m.d.w.
}```

2. The first step is consistent indentation.
Code:
```//     Purpose: Program plays a guessing game.
//        Program randomly generates a number in the domain of 1-100
//        Program picks a number between 1 and 100.  User guesses the number.
//        The program provides feedback for each guess.
//        When you guess correctly user is given feedback about the number of guesses.
//
//    Inputs:
//        User enters a guess each round.
//
//    Outputs--
//        Ask for initial guess between 1-100
//        Provide feedback for each guess: ice cold, cold, cool, warm, hot, boiling hot
//        When correct guess is made: Provide number of turns to reach correct.

//These go before main()

#include <stdio.h>              //Preprocessor Directive
#include <stdlib.h>             //Preprocessor Directive
#include <time.h>               //Preprocessor Directive

//main argument
int main()
{                               //Function-main argument
//Code for random number generator-Instructor's code.//
srand(time(NULL));            // Seed the random number generator

//variables
int goal = rand() % 100 + 1;  // goal will be 1-100, inclusive

int i = 1,                    // Index for guess counter
turn,                     // Guess counter for final answer.
guess,                    // User's guess.
distance;                 // Distance of guess from goal on the numberline.

printf("I have a number, 1-100.\n");
do {
//printf("Super Secret # %d / Guess # %d\n",goal, turn);    //used for testing--if/ else if
printf("What's your guess?  ");
scanf("%d", &guess);        //User inputs integer

distance = abs(goal - guess); //distance gets value.
turn = i++;                 //turn counter

printf("%d", guess);

if (guess < 1 || guess > 100) {
printf(" is not 1-100.\n", guess);
}
else if (distance > 50) {
printf(" is ice cold.\n");
}
else if (distance <= 50 && distance > 30) {
printf(" is cold.\n");
}
else if (distance <= 30 && distance > 20) {
printf(" is cool.\n");
}
else if (distance <= 20 && distance > 10) {
printf(" is warm.\n");
}
else if (distance <= 10 && distance > 2) {
printf(" is hot.\n");
}
else if (distance <= 2 && distance >= 1) {
printf(" is boiling hot.\n");
}
continue;                   //continue sends us back to the begining of do
}
while (guess != goal);        //while keeps us in loop until input is same as goal

printf(" is right in %d guesses.\n", turn);

return 0;                     //e.m.d.w.
}```
> turn = i++; //turn counter
Or just turn++ and delete an extra variable which has no use.

> continue; //continue sends us back to the begining of do
This too is unnecessary.
All it does is jump to the loop condition, but that is exactly where it was going to end up anyway.

3. Thanks for your feedback. What convention do you use for indention? I know there should be some indentation; but I'm not sure how to go abouyt it. Is it based on preference, convention or just something you pick up reading other people's code?

Thanks for the tips about the variable and continue. They were legacies of my previous versions of the project although until you pointed it out I did think they were necessary.

Thanks again!!!

jjm

4. Originally Posted by AxeSwinger
Thanks for your feedback. What convention do you use for indention? I know there should be some indentation; but I'm not sure how to go abouyt it. Is it based on preference, convention or just something you pick up reading other people's code?

Thanks for the tips about the variable and continue. They were legacies of my previous versions of the project although until you pointed it out I did think they were necessary.

Thanks again!!!

jjm
Check out this wikipedia page about indentation.

5. The first thing to do is make sure your text editor / IDE is set to "spaces for tabs". It's the only way to ensure a consistent look across the widest possible range of media. Mixed tabs and spaces are a disaster when posted.

The second point is simply to be consistent with your chosen style.

Popular pages Recent additions