loop

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
int main(void) {
//declaretion
int guess;
int i;
int lowerbound = 0;
int upperbound = 100;
int random;
int r = rand() % 2;
srand(time(0));
// welcoming the user and asking the user to guess a number
printf("Welcome to the game\n");
printf("guess the number between 0 and 100\n");
//scan for the number
scanf("%d", &guess);


for (i = 0; i < 10; i++) {
if (guess > 100 || guess < 1)
printf("you are guessing out of the range");
else if (guess >random + 50 || guess < r - 50)
printf("Not even close. Too far");
else if (guess >= r + 40 || guess <= r - 40)
printf("Far");
else if (guess >= r + 20 || guess <= r - 20)
printf("Hmmm, close");
else if (guess >= r + 10 || guess <= r - 10)
printf("TOO CLOSE!");
else if (guess >= r + 5 || guess <= r - 5)
printf("YOU'RE WAY TOO CLOSE!");
else if (guess >= r + 1 || guess <= r - 1)
printf("OMGGG YOU ALMOST GOT IT!");
else if (guess == r)
printf("DING DING DING YOU WIN!");
}
//for (i = 0; i < 10; i++)
//random = 0 + (rand) % input);
return 0;

}
hello, this program is a code for a game where the code is supposed to generate a random secret number between 1 and 100 and ask the user to guess the number and it should give the user 10 times to guess and it tells after each guess if the entered number is close or far from the number that the computer picked but instead of giving the player the chance to play 10 times to guess it is printing out the result so i dont know where is the nitake here
What does your loop do? It looks at the number guess ten times and says something about it.

Does it accept input from the user? No.
Does it do anything apart from look at guess and say something? No.

Did you mean your loop to be ten of this, as it currently is:
1
2
  look at number
  say something about it


or did you mean it to be ten of this:
1
2
3
  get number from user
  look at number
  say something about it


Do you see the difference there?
Last edited on
Hello iceman870,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

In addition to what Repeater said about you for loopI see soe other problems.

#define _CRT_SECURE_NO_WARNINGS If you have to use this you are doing something very wrong. Like maybe putting a C program in a .cpp file and compiling it as a C++ program.

int main(void) Is n old C style way of writing this line and is no longer used in modern code. It may at the least give you a warning if not an error at compile time.

The lines
1
2
int r = rand() % 2;
srand(time(0));
Are backwards. And this srand(time(0)); would be better written as srand(unsigned int (time(0))); "srand" only needs to be set once at the beginning of the program before "rand" is used.

The scanf("%d", &guess); is causing the need for #define _CRT_SECURE_NO_WARNINGS With my compiler the error message tells me to use scanf_s because it is more secure.

Your for loop deals with one entry for "guess" and does it 10 times because "guess" never changes. At the end of the for loop you need to input a new guess. A do/while or while loop is usually a better choice over the for loop, but it works if done right.

Just my thoughts on your code.

I am nor sure what the best search words would be, but there are many examples here of C++ code that on the sme type of program you could look at.

Hope that helps,

Andy
Last edited on
#define _CRT_SECURE_NO_WARNINGS If you have to use this you are doing something very wrong. Like maybe putting a C program in a .cpp file and compiling it as a C++ program.

Even if you compile as a C program VS gives you a stupid warning
Warning C4996 This function or variable may be unsafe. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
Last edited on
Hello Repeater,
yes it is supposed to accept input from user and also i mean
get number from user
look at number
say something about it

thanks
So inside the loop, get a number from the user. You already wrote code for that. You just need to put it inside the loop.
Topic archived. No new replies allowed.