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.
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 2 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}
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 years ago.
Improve this question
I made a program which takes a user to input number, if user inputed number less than 0 or greater than 100, then program returns to main function.
Here is my code:
#include <stdio.h>
int main(){
int a; scanf("%d", &a);
if(a > 100 || a < 0) {
printf("Going back to program.");
return main();
} else {
printf("Your number: %d\n", a);
}
}
But this code doesn't take a input and it just prints "Your number: 0".
So, what is wrong with this code?
Using recursion here (calling main again from main) is overly complicated and useless.
You want this:
#include <stdio.h>
int main()
{
int a;
do
{
scanf("%d", &a);
} while (a > 100 || a < 0); // simply repeat scan if a is out of bounds
printf("Your number: %d\n", a);
}
This is basic C knowledge. You probbaly should start reading your C textbook.
Your code is not good... A better way of doing it would be to use a loop
Better Solution
int main(){
int a;
do {
scanf("%d", &a);
} while(a > 100 || a < 0);
printf("Your number: %d\n", a);
return 0;
}
Your working solution
int main(){
int a;
scanf("%d", &a);
if(a > 100 || a < 0) {
printf("Recursion");//You are not going back you go further in depth (Recursion)
return main();
} else {
printf("Your number: %d\n", a);
return 0;//You need to return in if and in else
}
}
This is basic knowledge about programming (with c). I think you are a beginner so maybe you want to start with an Tutorial?
There is nothing wrong with your code, it should work.
scanf() reads from stdin. Depending on how and where your program runs stdin can have different meanings.
On tutorialspoint.com, you have to supply data to stdin before running your program by using the "Stdin" tab.
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 7 years ago.
Improve this question
How do you test for primes with the IsPrime method below? I cannot seem to get the printf to work in my IsPrime method and no errors were thrown.
#include <stdlib.h>
#include <stdio.h>
int IsPrime(unsigned int number) {
if (number <= 1) {
return 0; // zero and one are not prime
printf("zero and one are not prime.");
}
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) {
return 0;
printf("not a prime.");
}
}
return 1;
printf("You've found a prime!");
}
int main(void) {
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
if(a >= 1 && a <= 1000) {
printf("You entered: %d\n", a);
IsPrime(a);
}
else {
printf("Error! Please enter a value between 1 and 1000.");
}
}
You're return-ing from function before printf
You wrote printf statement after return; It's an easy fix, just swap the two instructions.
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 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 8 years ago.
Improve this question
I'm trying to write a small function that will get a number.
The function should be idiot-proof so it would give warnings if ie. someone entered a character instead.
I wrote a function like the one below, but if I enter a non-int the program gives me an infinite loop, constantly repeating the printf "Not a valid number" so I never get a chance to do the correct input.
The code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
for (int ret = 0; ret < 1;)
{
int num;
printf("\n Please input a number: ");
ret = scanf ("%d", &num);
if (ret < 1)
printf ("\nNot a valid number!");
else
printf("\nYou input %d", num);
}
return 0;
}
How to fix it?
Replace this line if (y = 0) with this if (y == 0).
Note the line below with the comment about eating the input buffer. Since your scanf didn't find what it is looking for in the input buffer, the wrong input just stays there and fails forever unless you do something to "eat" it.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Hello world!\n");
while ('A')
{
int x, y;
printf("\n x: ");
y = scanf ("%d", &x);
printf("\nx = %d", x);
if (y < 1)
{ // eat the input buffer so we can try again
while ( getchar() != '\n' );
printf ("\nWRONG!");
}
}
return 0;
}