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);
}
Related
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I am running a hollow box statement and cant figure out the error message
error: expected identifier or ‘(’ before ‘{’ token" {
I have tried multiple variations of the bracket in different positions and it just causes more errors. Here is the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int i;
printf("Even numbers between 25 to 75: \n");
for (i = 25; i<=75; i++)
{
if(i%2 == 0)
{
printf("%d\t", i);
}
}
printf("All odd numbers between 500 to 400: \n");
for (i = 500; i>=400; i--)
{
if(i%2 == 0)
{
printf("%d\t", i-1);
}
}
int number, result, exponent;
result = 1;
printf("Enterthe base number: ");
scanf("%d", &number);
printf("Enter the exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= number;
--exponent;
}
printf("Answer = %d \n", result);
}
int f, w;
{
for (f = 1; f <= 7; f++)
{
for (w = 1; w <= 7; w++)
{
if (f==1 || f==7 || w==1 || w==7)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This is an update to the first question since my first post didnt show the whole code and everyones answer was asking for it.
You should put an int main(void) before the body of your main function. You can then move your variables inside the function. After you've done this, the top of your code should look like:
int main(void) /* Here! */
{
int f, w; /* Move this inside the function. */
for (f = 1; f <= 7; f++)
...
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
Why should I write the statement "while(scanf("%d",&t)==1)" on the online judges ? Why I get WRONG verdict if i Submit without this statement ? As my IDE (Code blocks compiler) doesn't find any error .
#include<stdio.h>
int main()
{
int t,i;
float h,l,w;
while(scanf("%d",&t)==1)
{
for(i=1;i<=t;i++)
{
scanf("%f%f%f",&l,&w,&h);
if (l<=20 && w<=20 && h<=20)
printf("Case %d: good\n",i);
else
printf("Case %d: bad\n",i);
}
return 0;
}
}
"While" loop isn't necessary. Your code works perfectly fine without while loop, if you write like this:
scanf("%d", &t);
for (i = 1; i <= t; i++)
If you want multiple "t" inputs you should move your return statement after while brace:
int t, i;
float h, l, w;
while (scanf("%d", &t)) {
for (i = 1; i <= t; i++)
{
scanf("%f%f%f", &l, &w, &h);
if (l <= 20 && w <= 20 && h <= 20)
printf("Case %d: good\n", i);
else
printf("Case %d: bad\n", i);
}
}
return 0;
Also, checking if scanf returned 1 is some kind of protection. scanf returns number of elements filled (int this case 1). If you try to write non-digits it returns 0. You can check what your scanf returns with this or similar code:
printf("%d", scanf("%d", &t));
Good luck!
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)
...
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;
}