The program should accept input values on stdin until EOF is reached. The input is guaranteed to be well-formed and contain at least one valid floating point value.
Sample input:
3.1415
7.11
-15.7
Expected output:
3 3 4
7 7 8
-16 -16 -15
Done.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
for(;;)
{
float b;
scanf("%f", &b);
printf("%g %g %g\n",floor(b), round(b), ceil(b));
int i=0;
int result = scanf("%d", &i);
if( result == EOF)
{
printf("Done.\n");
exit(0);
}
}
return 0;
}
My program only runs one time. After that it outputs 0 1 1
If you want to stay with the scanf, you can use the return value to detect a erroneous/EOF input:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int result = 0;
for(;;)
{
float b;
result = scanf(" %f", &b);
if(result == EOF)
break;
printf("%g %g %g\n",floor(b), round(b), ceil(b));
}
printf("Done");
return 0;
}
Try it here.
Also a look at this question is helpful.
I think your second scanf is the problem, like others have already told you. How about doing it this way? The getchar() call seems to pick up the EOF more reliably.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void) {
float b;
for (;;) {
scanf("%f",&b);
printf("%g %g %g\n",floor(b),round(b),ceil(b));
if (getchar() == EOF)
break;
}
return 0;
}
Related
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
I have a problem, I want to make the user enter a digit from 1.00 to 10.00 and if he enters a digit outside of that range I will display that he was wrong and try again. I think I have done well but the problem is if the user enters a letter the loop repeats at infinity, I would appreciate your help. Thank you. :)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
scanf("%f", &a);
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
system("pause");
return 0;
}
scanf leaves the '\n' char in the input stream so you need to skip it:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
scanf(" %f", &a);
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
printf("\nyou have entered: %f\n", a);
return 0;
}
https://godbolt.org/z/baPxdn .But it does not sort out the wrong input problem.
I personally prefer to read the line and then scan it (and it does sort all the problems out):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char str[100];
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
fgets(str, 100, stdin);
if(sscanf(str, " %f", &a) != 1) continue;
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
printf("\nyou have entered: %f\n", a);
return 0;
}
https://godbolt.org/z/vxY7To
When you try to run scanf() on invalid input, it will not update a because it's impossible, so your loop will continue forever since a is never updated and the scanf function will fail forever, nothing is actually "read" by scanf, and never will never get past the invalid input.
Make sure you handle the invalid inputs by "consuming" them before repeating the loop.
You have to check the result of the 'scanf', and "clean" the IO stream buffer once something error happen.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define CLEAN_STD_STREAM() \
do{\
int ch; \
while ((ch = getchar()) != '\n' && ch != EOF); \
}while(0)
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
if(scanf("%f", &a) != 1)
{
CLEAN_STD_STREAM();//fflush() dosen't work on the Linux
continue;
}
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
system("pause");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int i;
int result;
float f;
while((result = scanf("%d", &i)) != EOF)
{
scanf("%f", &f);
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
Hi,
I just began with C and I'm having a problem solving a question.
The problem is that with the user input, I need to get three sets of numbers that are floored, rounded, and ceiled. This process must be ongoing until the user stops by EOF command (Ctrl-D).
When I run my code above, input a value 3.1415, I get 0 0 1 as an output, which is wrong because it's supposed to be 3 3 4.
Any suggestions or help on how to fix the problem would be appreciated.
According to your code, you first need to input an integer value and then enter a float value.
OR, you can start accepting float value like this:
#include <stdio.h>
#include <math.h>
int main()
{
int result;
float f;
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
return 0;
}
According to your code you need to input integer first then float, But if you enter float value first, that value read by i first and return 0 that is !=EOF, so second scanf does not wait for input, because it is inside the while loop. So always you will get 0 0 1 for all inputs!
To scan a number inside while loop use-
if (scanf("%f", &f) == 0) {
printf("Err. . .\n");
do {
c = getchar();
}
while (!isdigit(c));
ungetc(c, stdin);
Else scan float value first instead of int and float. Try this code-
while((result = scanf("%f", &f)) != EOF)
{
printf("%.0f %.0f %.0f\n", floor(f), round(f), ceil(f));
}
printf("Done.\n");
Write a program that displays a new random permutation of the integers 0 to 9 at the request of its user. For example, the program’s output could be as follows:
Your program should prints how many 7 was printed when user type no.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i , total , r;
char ans;
srand(time(NULL));
do{
for( i=0 ; i < 10 ; i++)
{
r= (rand()%(9-1+1)) + 1;
printf ("%d ",r);
}
total =0;
if (r==7) // Here how can I correct this so total will increase every time
{ // there is a 7 in the string
total++;
}
printf("\nAnother permutation: y/n?\n");
scanf(" %c",&ans);
if (ans != 'y')
{
printf("Bye!\n");
printf("The number of 7's is: %d", total);
}
}while(ans=='y');
return 1;
}
I have a problem with my code. How can I increment the 7's shown in this program after != 'y'.
Set total=0 before entering into the do-while loop, to get the correct total.