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.
}