Guessing number game in C programming [closed]
up vote
-2
down vote
favorite
When I run my program, the guessing is broken. The number has to be entered twice before it goes to any of the conditions, and when the right number is guessed, it does not register as correct and only will until it is guessed again.
This is my code:
/*Intro to C
November 1, 2018
Overview: Write a simple game to be played between the computer and a single user.
The user defines a sample space for the computer to choose a secret number from.
The user then attempts to guess the number and is told “too high” or “too low” after each guess. (1, 2, 3, 5, 6, 8, 10, 12, 13)
Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard int type. However, check to make sure the input was greater than 1 - otherwise, ask for the input again.
Accept integer input from the user for their guess of what the secret number might be.
If the user’s guess is correct, print a message saying that they guessed the number correctly and end the program.
If the user’s guess is lower than the secret number, print a message indicating their guess was too low, and accept another input.
If the user’s guess is higher than the secret number, print a message indicating their guess was too high, and accept another input.
If the user guesses a number outside the original range, or at any point makes a guess that contradicts the computer’s feedback, they lose and the game should end.
Example: Range is 1-100. Secret number is 22. User guesses 50. Computer indicates guess is too high. User guesses 55. User loses since they ignored the computer’s instruction about 50.
Example: Range is 1-100. Secret number is 49. User guesses 35. Computer indicates guess is too low. User guesses 70. Computer indicates guess is too high. User guesses 30. User loses since they ignored the computer’s previous instruction about 35.
Use at least two functions where appropriate. Do not put all of your code in the main function.
Test your program thoroughly. For testing purposes, you could add a print message showing what the secret number is, so that you can make guesses knowing what the output should be. Remove any such print message before turning in the final version of your program.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//Global Var
int input;
//Functions
void hello();
int upper();
int guessing(int input);
int main(void) {
//Prompt function
hello();
//Input upper bound function
upper();
//Gussing function
guessing(input);
printf("n");
system("pause");
return 0;
}
void hello()
{
//Prompt
printf("Hello! This program is a guessing game.n");
printf("I will start at 1. Please enter in an upper bound for me to guess a number (example: 10): n");
}
int upper()
{
//User input upper
scanf("%d", &input);
return input;
}
int guessing(int input)
{
//Choosing Secret Number
srand(time(NULL));
int secret = rand() % input;
//Check Secret Number
printf("%dn", secret);
//Guessing
printf("I've got my secret number; try and guess what it is! You have unlimited tries:n");
//Loop
while (1) {
int guess;
scanf("%dn", &guess);
//Conditions
if (guess < secret)
{
printf("Too low!n");
}
else if (guess > secret)
{
printf("Too high!n");
}
else if (guess == secret)
{
printf("You got it!n");
break;
}
else if (guess > input && guess < input)
{
printf("ERROR. Restart and try again.n");
}
else
{
printf("ERROR. Restart and try again.n");
break;
}
}
return 0;
}
c number-guessing-game
closed as off-topic by 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight Nov 15 at 18:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-2
down vote
favorite
When I run my program, the guessing is broken. The number has to be entered twice before it goes to any of the conditions, and when the right number is guessed, it does not register as correct and only will until it is guessed again.
This is my code:
/*Intro to C
November 1, 2018
Overview: Write a simple game to be played between the computer and a single user.
The user defines a sample space for the computer to choose a secret number from.
The user then attempts to guess the number and is told “too high” or “too low” after each guess. (1, 2, 3, 5, 6, 8, 10, 12, 13)
Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard int type. However, check to make sure the input was greater than 1 - otherwise, ask for the input again.
Accept integer input from the user for their guess of what the secret number might be.
If the user’s guess is correct, print a message saying that they guessed the number correctly and end the program.
If the user’s guess is lower than the secret number, print a message indicating their guess was too low, and accept another input.
If the user’s guess is higher than the secret number, print a message indicating their guess was too high, and accept another input.
If the user guesses a number outside the original range, or at any point makes a guess that contradicts the computer’s feedback, they lose and the game should end.
Example: Range is 1-100. Secret number is 22. User guesses 50. Computer indicates guess is too high. User guesses 55. User loses since they ignored the computer’s instruction about 50.
Example: Range is 1-100. Secret number is 49. User guesses 35. Computer indicates guess is too low. User guesses 70. Computer indicates guess is too high. User guesses 30. User loses since they ignored the computer’s previous instruction about 35.
Use at least two functions where appropriate. Do not put all of your code in the main function.
Test your program thoroughly. For testing purposes, you could add a print message showing what the secret number is, so that you can make guesses knowing what the output should be. Remove any such print message before turning in the final version of your program.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//Global Var
int input;
//Functions
void hello();
int upper();
int guessing(int input);
int main(void) {
//Prompt function
hello();
//Input upper bound function
upper();
//Gussing function
guessing(input);
printf("n");
system("pause");
return 0;
}
void hello()
{
//Prompt
printf("Hello! This program is a guessing game.n");
printf("I will start at 1. Please enter in an upper bound for me to guess a number (example: 10): n");
}
int upper()
{
//User input upper
scanf("%d", &input);
return input;
}
int guessing(int input)
{
//Choosing Secret Number
srand(time(NULL));
int secret = rand() % input;
//Check Secret Number
printf("%dn", secret);
//Guessing
printf("I've got my secret number; try and guess what it is! You have unlimited tries:n");
//Loop
while (1) {
int guess;
scanf("%dn", &guess);
//Conditions
if (guess < secret)
{
printf("Too low!n");
}
else if (guess > secret)
{
printf("Too high!n");
}
else if (guess == secret)
{
printf("You got it!n");
break;
}
else if (guess > input && guess < input)
{
printf("ERROR. Restart and try again.n");
}
else
{
printf("ERROR. Restart and try again.n");
break;
}
}
return 0;
}
c number-guessing-game
closed as off-topic by 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight Nov 15 at 18:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
1
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
When I run my program, the guessing is broken. The number has to be entered twice before it goes to any of the conditions, and when the right number is guessed, it does not register as correct and only will until it is guessed again.
This is my code:
/*Intro to C
November 1, 2018
Overview: Write a simple game to be played between the computer and a single user.
The user defines a sample space for the computer to choose a secret number from.
The user then attempts to guess the number and is told “too high” or “too low” after each guess. (1, 2, 3, 5, 6, 8, 10, 12, 13)
Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard int type. However, check to make sure the input was greater than 1 - otherwise, ask for the input again.
Accept integer input from the user for their guess of what the secret number might be.
If the user’s guess is correct, print a message saying that they guessed the number correctly and end the program.
If the user’s guess is lower than the secret number, print a message indicating their guess was too low, and accept another input.
If the user’s guess is higher than the secret number, print a message indicating their guess was too high, and accept another input.
If the user guesses a number outside the original range, or at any point makes a guess that contradicts the computer’s feedback, they lose and the game should end.
Example: Range is 1-100. Secret number is 22. User guesses 50. Computer indicates guess is too high. User guesses 55. User loses since they ignored the computer’s instruction about 50.
Example: Range is 1-100. Secret number is 49. User guesses 35. Computer indicates guess is too low. User guesses 70. Computer indicates guess is too high. User guesses 30. User loses since they ignored the computer’s previous instruction about 35.
Use at least two functions where appropriate. Do not put all of your code in the main function.
Test your program thoroughly. For testing purposes, you could add a print message showing what the secret number is, so that you can make guesses knowing what the output should be. Remove any such print message before turning in the final version of your program.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//Global Var
int input;
//Functions
void hello();
int upper();
int guessing(int input);
int main(void) {
//Prompt function
hello();
//Input upper bound function
upper();
//Gussing function
guessing(input);
printf("n");
system("pause");
return 0;
}
void hello()
{
//Prompt
printf("Hello! This program is a guessing game.n");
printf("I will start at 1. Please enter in an upper bound for me to guess a number (example: 10): n");
}
int upper()
{
//User input upper
scanf("%d", &input);
return input;
}
int guessing(int input)
{
//Choosing Secret Number
srand(time(NULL));
int secret = rand() % input;
//Check Secret Number
printf("%dn", secret);
//Guessing
printf("I've got my secret number; try and guess what it is! You have unlimited tries:n");
//Loop
while (1) {
int guess;
scanf("%dn", &guess);
//Conditions
if (guess < secret)
{
printf("Too low!n");
}
else if (guess > secret)
{
printf("Too high!n");
}
else if (guess == secret)
{
printf("You got it!n");
break;
}
else if (guess > input && guess < input)
{
printf("ERROR. Restart and try again.n");
}
else
{
printf("ERROR. Restart and try again.n");
break;
}
}
return 0;
}
c number-guessing-game
When I run my program, the guessing is broken. The number has to be entered twice before it goes to any of the conditions, and when the right number is guessed, it does not register as correct and only will until it is guessed again.
This is my code:
/*Intro to C
November 1, 2018
Overview: Write a simple game to be played between the computer and a single user.
The user defines a sample space for the computer to choose a secret number from.
The user then attempts to guess the number and is told “too high” or “too low” after each guess. (1, 2, 3, 5, 6, 8, 10, 12, 13)
Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard int type. However, check to make sure the input was greater than 1 - otherwise, ask for the input again.
Accept integer input from the user for their guess of what the secret number might be.
If the user’s guess is correct, print a message saying that they guessed the number correctly and end the program.
If the user’s guess is lower than the secret number, print a message indicating their guess was too low, and accept another input.
If the user’s guess is higher than the secret number, print a message indicating their guess was too high, and accept another input.
If the user guesses a number outside the original range, or at any point makes a guess that contradicts the computer’s feedback, they lose and the game should end.
Example: Range is 1-100. Secret number is 22. User guesses 50. Computer indicates guess is too high. User guesses 55. User loses since they ignored the computer’s instruction about 50.
Example: Range is 1-100. Secret number is 49. User guesses 35. Computer indicates guess is too low. User guesses 70. Computer indicates guess is too high. User guesses 30. User loses since they ignored the computer’s previous instruction about 35.
Use at least two functions where appropriate. Do not put all of your code in the main function.
Test your program thoroughly. For testing purposes, you could add a print message showing what the secret number is, so that you can make guesses knowing what the output should be. Remove any such print message before turning in the final version of your program.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//Global Var
int input;
//Functions
void hello();
int upper();
int guessing(int input);
int main(void) {
//Prompt function
hello();
//Input upper bound function
upper();
//Gussing function
guessing(input);
printf("n");
system("pause");
return 0;
}
void hello()
{
//Prompt
printf("Hello! This program is a guessing game.n");
printf("I will start at 1. Please enter in an upper bound for me to guess a number (example: 10): n");
}
int upper()
{
//User input upper
scanf("%d", &input);
return input;
}
int guessing(int input)
{
//Choosing Secret Number
srand(time(NULL));
int secret = rand() % input;
//Check Secret Number
printf("%dn", secret);
//Guessing
printf("I've got my secret number; try and guess what it is! You have unlimited tries:n");
//Loop
while (1) {
int guess;
scanf("%dn", &guess);
//Conditions
if (guess < secret)
{
printf("Too low!n");
}
else if (guess > secret)
{
printf("Too high!n");
}
else if (guess == secret)
{
printf("You got it!n");
break;
}
else if (guess > input && guess < input)
{
printf("ERROR. Restart and try again.n");
}
else
{
printf("ERROR. Restart and try again.n");
break;
}
}
return 0;
}
c number-guessing-game
c number-guessing-game
asked Nov 15 at 17:55
Kim Lam
4
4
closed as off-topic by 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight Nov 15 at 18:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight Nov 15 at 18:54
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, chux, Sᴀᴍ Onᴇᴌᴀ, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
1
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09
add a comment |
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
1
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
1
1
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
All the conditions also seem to not be working (only the first three work?)
– Kim Lam
Nov 15 at 18:04
1
Welcome to Code Review! Unfortunately this post is off-topic for this site. Please read What topics can I ask about here? - note that it states "If you are looking for feedback on a specific working piece of code...then you are in the right place!" Also, when posting your question, there should have been text on the side that read "Your question must contain code that is already working correctly..." When you have fixed the code, please update it here and it can be reviewed.
– Sᴀᴍ Onᴇᴌᴀ
Nov 15 at 18:09