Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
In my homework user selects a number between 1 and 20 but at a chance of %50 computer can hepls wrongly.For example selected number:15 user guess:10 computer may say(guess lower number) with a %50 chance. I tried to create a chance by using another random but it selects a constant number so program always say guess higher or lower.Am I thinking wrongly ? Please help me to solve this problem.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int randomNum = (rand() % 20) + 1;
int c = (rand() % 20) + 1;
int i = 1;
int userGuess = 0;
printf("Guess a number between 1 to 20 \n"); //start with guess
scanf(" %d", &userGuess);
while (randomNum != userGuess) {
if (userGuess > randomNum && c % 2 == 0) {
printf("Guess lower number \n");
scanf(" %d", &userGuess);
} else
if (userGuess > randomNum && c % 2 != 0) {
printf("Guess higher number \n");
scanf("%d", &userGuess);
} else
if (userGuess < randomNum && c % 2 == 0) {
printf("Guess higher number \n");
scanf(" %d", &userGuess);
} else
if (userGuess < randomNum && c % 2 != 0) {
printf("Guess lower \n");
scanf("%d", &userGuess);
}
i++;
return 0;
}
You need to get a new random number c for the 50% condition each loop.
Move the line like int c= (rand()%20)+1; into the loop so that it gets a fresh number each time.
But you don't need to do the %20 or +1 on c. You are doing % 2 in the if() statement. Don't add 1 since you are already comparing it to 0 or != 0. So something like:
while (randomNum!=userGuess)
{
int c = rand();
if(userGuess>randomNum && c%2 == 0)
...
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I am trying to make a program that detects a user input and prints stuff based on that input. The input needs to have limits set however the if statements I set up are being ignored and I'm not quite sure why
#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
int main(void)
{
int num;
int exit;
exit = 0;
printf("\nLab5 p1 by Chung - En Hou\n");
num = 0;
do{
printf("\nEnter a number between 0 and 255:");
//input
scanf("%d", &num);
//ignored if statement?
if (num < 256 || num >= 0) {
Sleep(250);
printf("%d\n", num);
Sleep(250);
printf("%x\n", num);
Sleep(250);
printf("%c\n", num);
printf("Press any button to Run the program again or press Esc to exit");
exit = getch();
}
//else also ignored
else {
printf("\nthat number is turbo cringe please try again\n");
printf("\nPress any button to Run the program again or press Esc to exit\n");
exit = getch();
}
} while (exit != 27);
return 0;
}
I think you mean
if (num < 256 && num >= 0) {
instead of
if (num < 256 || num >= 0) {
That is the if statement checks whether the value of num is in the range [0, 255]
As for the expression in the if statement shown in the question then it will evaluate to true for any integer value of the variable num.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Can anyone identify why the line : if(code == rand() && attempt != 3) not executed even I'm entered a correct code that generated by the rand().
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
int code, attempt = 0;
srand(time(0));
for(int i = 0; i<1; i++)
printf("Your code is %d ", rand());
do{
printf("\nEnter code: ");
scanf("%d", &code);
attempt++;
}while ( code != rand() && attempt != 3);
if(code == rand() && attempt != 3)
{
printf("\n Correct code");
printf("\n");
}
else
{
printf("Incorrect code\n");
}
return 0;
}
The rand function is a random number generator. It returns a different number each time you call it.
Call rand once at the start of your program and save the result in a variable. Then check the user's guess against that.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Not doing anything special. Just taking a user's input between 1 - 500, and then printing the number using for loop for each iteration. It crashes at the for loop. It does not print anything at all.
#include <stdio.h>
int forCounter() {
int num;
int count = 0;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
int i = num;
for (i; count <= i; count++) {
printf(count);
}
getchar();
return 0;
}
I take first input applied to 'num' and check if it's above or below allowed amount with the while loop.
When that's done it leaves and should start the for loop with i taking over for num. I tried using num in the place of i but it didn't work so I tried using a separate variable to see if it'd work.
I get two warnings seen in the image providedTwo warnings
You have to specify the format of output in printf.
int printf(const char *format, ...)
Your code:
#include <stdio.h>
int forCounter(void) {
int num;
printf("Pick a positive number (1 - 500): ");
scanf("%d", &num);
while (num < 1 || num > 500) {
printf("Out of range, try again (1 - 500): ");
scanf("%d", &num);
}
for (int i = 1; i <= num; i++) {
// printf(count); --> Bad
printf("Value = %d\n", i);
}
getchar(); // this will return immediately
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Here is the code. Very basic and should work properly:
int main() {
int n, status, i;
printf("Please enter an integer bigger than 1: ");
status = scanf("%d", &n);
if (status != 1 || n > 1) {
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) {
printf("What is going on %d\n", i);
}
return 0;
}
The program compiles good, and after I insert "10" as input, the program does nothing. Just as if it was supposed to return 0 without doing nothing.
EDIT: The main lesson here is don't post questions when it's late and you're super tired. The accepted answer (and handful of friendly comments) show why
Do you not see the problem here?
if (status != 1 || n > 1) { // n can not be larger than 1.
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) { // n must be larger than 2.
printf("What is going on %d\n", i);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need to write a program that plays the game "guess the number" when the computer selects the number from 1 to 100, and the user has 10 attempts, each round the user receives output if the number he guessed is too small or too big, or if its the lucky number your help please
thats what i did so far...
#include <stdio.h>
#include <stdlib.h>
void main() {
int i ,guess=0 , lucky=rand()%100;
for (i = 0; i <= 10; i++) {
printf("please enter your guess\n");
scanf("%d",&guess);
}
}
This should work for you!
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int count , guess = 0, lucky = rand() % 101;
for (count = 0; count < 10; count++) {
printf("please enter your guess\n>");
scanf("%d", &guess);
if(guess == lucky) {
printf("WIN");
break;
}
if(guess > lucky)
printf("too big\n\n");
else
printf("too small\n\n");
}
if(count == 10)
printf("FAIL");
return 0;
}