C- number checking function gives infinite loop [closed] - c

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;
}

Related

If statement in a do while loop being ignored [closed]

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.

how can i copy string to another variable and output with if else statement [closed]

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
#include<stdio.h>
#include<string.h>
int main(void)
{
//Assigning string to name1//
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
//Assigning string to name2//
char name2[7] = "Mudus";
//prompt for input//
int i;
printf("Choose 1 or 0: ");
scanf("%i \n", &i);
}
//for copying string of name1 to copyname//
if (i = 0)
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
I want to copy the string to another variable and output with if-else condition through this way only but I am getting error.
The output is the expected identifier or ‘(’ before ‘if’
You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char name1[8] = "Aaeric";
//will be used to copy name1 to copyname//
char copyname[8];
char name2[7] = "Mudus";
int i;
printf("Choose 1 or 0: ");
scanf("%i", &i);
if (i == 0) // <<<<< use == instead of =
{
strcpy(copyname, name1);
puts(copyname);
return 0;
}
else
puts(name2);
}
poinless comments removed
code snippets moved into main
code formatted properly
scanf("%i \n", &i); replaced with scanf("%i", &i);
if (i = 0) replaced with if (i == 0)

Counting the amount of digits in a while loop does not work [closed]

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;
}

C recursion practice won't display output [closed]

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));
}

C - What is wrong with this code? [closed]

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.

Resources