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.
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 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 3 years ago.
Improve this question
This is my attempt at recursion. It compiles and runs, but doesn't display the factorial of the number that I input. I'm attempting this with Geany on Ubuntu.
#include <stdio.h>
int fact(int n);
int main() {
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
int fact(int n) {
if (n <= 1)
return 1;
else
return n * fact(n - 1);
}
You are missing the print statement.
You could save the result in a variable and then print it.
printf("%d",fact(n));
Your code is fine, but you forget to to print the return value of function fact() change this portion of your code
scanf("%6d", &n);
fact(n);
to this:
scanf("%6d", &n);
printf("%d", fact(n));
Your work will done. after replacing your main() function will look like:
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
printf("%d", fact(n));
}
Note: The factorial of 17 or higher is not adjust in integer limit.
doesn't display the factorial of the number that I input.
in
int main()
{
int n;
printf("Give me a number");
scanf("%6d", &n);
fact(n);
}
you do not print the result of factorial, then it is not print
why are you using "%6d" rather than "%d" in the scanf ? you do not print so you do not need that
I also encourage you
to add a separator after Give me a number else the input number will seem attached to it (I used a ':' below)
to test the result of scanf
So, for instance :
int main()
{
int n;
printf("Give me a number:");
if (scanf("%d", &n) == 1)
printf("fact(%d)=%d\n", n, fact(n));
}
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
A have a problem with my program. I dont know what im doing wrong but loop do while doesnt work. At and program should ask "If you want to run this program again, press T.Other key should close this program.
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int main()
{
int a;
int b;
int c;
int f;
int g;
int h;
int d = 0;
char e;
srand(time(0));
do {
printf("How many numbers do you want to show: ");
scanf_s("%i", &a);
printf("od: ");
scanf_s("%i", &b);
printf("do: ");
scanf_s("%i", &c);
h = c + 1;
f = b - h;
for (d; d < a; d++) {
printf("%i ", b + rand() % f);
}
printf("\n");
printf("Restart program? T- Yes");
scanf_s("%s", &e);
} while (e == 't');
_getch();
return 0;
}
Program works fine, but when i press T at the end. it will close. Im using Visual Studio 2015
Now my code is below:
do {
printf("How many numbers do u want: ");
scanf_s("%i", &a);
printf("od: ");
scanf_s("%i", &b);
printf("do: ");
scanf_s("%i", &c);
h = c + 1;
f = b - h;
//printf("%i %i %i\n", h, f);
for (d; d < a; d++) {
printf("%i ", b + rand() % f);
}
printf("\n");
printf("Restart? T- yes");
scanf_s("%c", &e);
} while (e == 't' || e == 'T');
_getch();
return 0;
}
But it still doesnt work. I cant enter any letter. When i press any key a windows is going to close
As others have pointed, this is because you're trying to get a "%s" and, therefore, you can't compare it to a character. Use "%c" instead, or use strcmp function to compare 2 char arrays.
By the way, be aware that scanf_s is a Microsoft only function. Not sure if visual studio is forcing you to use it, but the common usage of scanf wouldn't hurt, check it out:
scanf("%c", &e);
Do you press 'T'? because 'T' and 't' are not the same character. check the ascii table
Your program will continue while you press a 't' so with a 'T' or anything else it will stop.
ps: You may want to use "read(0, &e, 1);" or "scanf_s("%c", &e);" instead of "scanf_s("%s", &e);" (last line of do while), because you have a char and if you enter a string like "Mr T. says yes" it will overwrite your memory which is never good.As a reminder char is 1 byte in memory, with '&' you can access its place in memory, so it acts like a string but if you write more than 1 character it will just write in the following memory which isn't attributted to your program. it will probably do nothing but it can overwrite another of your variable or worse overwrite another program in your computer (hopefully not something important like a window's program or else!)
char and string basics
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;
}