How to add the numbers inside the scanf in loop? - c

Is there some way to add all the numbers in a scanf loop? The loop would stop if the input number is negative. The problem is that the negative must also be included in the sum.
Here, I managed to get the sum of all the positive scanf values repeated in the loop, but the negative number is still not included in the sum of all the numbers.
#include <stdio.h>
main()
{
int z, x;
printf("Enter a number:\n");
z = 0;
scanf("%d", &x);
do
{
z += x;
scanf(" %d", &x);
} while (x >= 0);
printf("Sum = %d ", z);
return 0;
}

A simple rearrangement of the order of statements in your do ... while loop (and removal of the preceding scanf call) will do the trick:
#include<stdio.h>
int main() // You are returning "0" so declare "int" as return type
{
int x = 0, z = 0; // Easier to initialize at the same time as declaration.
printf("Enter a number:\n");
// scanf ("%d", &x); // Don't read here - do that inside the loop.
do {
int test = scanf(" %d", &x); // Read as the FIRST statement in the loop ...
if (test != 1) { // If the "scanf" call failed, we need to clear the input stream ...
int c;
while ((c = getchar()) != '\n' && c != EOF)
; // Clear any offending input
if (c == EOF) break; // Probably can't recover from an EOF, so exit the while loop
}
else z += x; // ... then we can add X even if it's negative
} while (x >= 0); // But end the loop when it IS negative anyway
printf("Sum = %d ", z);
return 0;
}
Note that I have added a test variable to makes sure that the scanf operation succeeded. If the usser enters foo (as mentioned in the comment by William Pursell), then the input buffer is cleared, the addition is skipped, and the read will be attempted again.

I'm not sure if you want to handle an input stream that does not contain a negative value as an error, but you could simply do:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
int sum = 0, x, rv;
while( 1 == (rv = scanf ("%d", &x)) ){
sum += x;
if( x < 0 ){
break;
}
}
if( rv == 0 ){
fprintf(stderr, "Invalid input\n");
} else {
printf("Sum = %d\n", sum);
}
return rv == 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
I think the following behavior is reasonable:
$ echo 4 5 8 5| ./a.out
Sum = 22
$ echo 4 5 -1 5| ./a.out
Sum = 8
$ echo 4 5 not-an-integer 5| ./a.out
Invalid input

Inverting the lines inside the loop should do it:
int z, x;
int ch;
printf("Enter a number:\n");
z = 0;
do
{
if (scanf("%d", &x) == 1) // checking if parsing was successful
{
z += x; // if so, perform the sum
}
else
{
puts("Bad input"); // if user inputs a bad value
while((ch = getchar()) != '\n' && ch != EOF){} // you clear the input buffer
}
} while (x >= 0);
printf("Sum = %d ", z);
Note that I also removed the first scanf ouside the loop, which becomes unneeded.

Related

Counting the number of zero in an integer

The program would ask the user an integer input.
and it counts how many zero the int has.
constraints: use while loop
ex:
input: 2400
count: 2
now I have no problem in that part, only when the user would input a zero.
supposed it counts 1.
ex:
input 0
count: 1
but then the program returns count 0.
here's the code:
int main(){
int n, counter = 0;
printf("Enter the number: ");
scanf("%d", &n);
while(n != 0){
if(n % 10 == 0){
counter ++;
n=n/10;
}else{
break;
}
}
printf("%d", counter);
return 0;
}
Use functions.
int countZeroes(int x)
{
int result = !x; // if x == 0 then result = 1
while(x)
{
result += !(x % 10);
x /= 10;
}
return result;
}
int main(void)
{
printf("%d\n", countZeroes(0));
printf("%d\n", countZeroes(1000));
printf("%d\n", countZeroes(-202020));
}
https://godbolt.org/z/91hKr46eo
You have while(n != 0) this does so when you enter just 0 it doesn't run. So the counter that you have set to 0 at the beginning is still 0
Here is what I would have done :
int main()
{
int num, count = 0;
scanf("%d",&num);
if (num == 0) {
printf("1");
return 0;
}
while(num != 0) //do till num greater than 0
{
int mod = num % 10; //split last digit from number
num = num / 10; //divide num by 10. num /= 10 also a valid one
if(mod == 0) count ++;
}
printf("%d\n",count);
return 0;
}
Just don't forget to consider everything that can happen with a condition that you set
**Fixed it
A different version that prints the integer as a string, and looks for '0' characters in it. Tested.
#include <stdio.h>
#include <string.h>
int main(void)
{
int input = 0;
int zeroes = 0;
char *foundpos, teststring[100];
scanf("%d", &input);
sprintf(teststring, "%d", input);
foundpos = strchr(teststring, '0');
while (foundpos != NULL) {
++zeroes;
foundpos = strchr(foundpos + 1, '0');
}
printf("%d contains %d zeroes", input, zeroes);
}
Just count the zero digits you get between \n chars.
#include <stdio.h>
int main()
{
int ndigs = 0, c;
while ((c = getchar()) != EOF) {
switch (c) {
case '0': ndigs++;
break;
case '\n': printf(" => %d zero digs", ndigs);
ndigs = 0;
break;
}
putchar(c);
}
}
sample output:
$ ./a.out
123001202010
123001202010 => 5 zero digs
^D
$ _
No need to convert digits to a number, to convert it back to decimal digits. You can improve the program counting digits until a nondigit is detected, then output. But there's no need to convert a decimal representation of a number (in base 10) to internal representation to then get the digits you have destroyed (in the conversion) back again to count them.
As earlier mentioned, the problem is with the loop:
while(n != 0){
if(n % 10 == 0){
counter ++;
n=n/10;
}else{
break;
}
}
It doesnt do anything in case n == 0. But replacing it with n > 0 is not a good solution because ints can be negative too.
You should use do{}while() construction instead, it will always do one iteration of loop no matter what condition you put there. Notice that no matter what you get as a number, it is still a number so you can do one iteration of loop either way.
Just do as follows:
do{
if(n % 10 == 0){
counter ++;
n=n/10;
}else{
break;
}
} while( n != 0 );
This should work(if i didnt mess up the braces/semicolumns).

Why does this code produce 1 on adding characters in C?

I was trying to restrict user input from alphabets (Eg: repeat input until correct input is provided ) , in order to get only numbers to be added. Somehow, instead of able to do so, I was able to add alphabets, but with out being '1'
It increments 1 if a number with a character is given.
#include<stdio.h>
int main() {
int x,y;
while(1)
{
printf("Enter a number > ");
if(scanf("%d%d",&x,&y) != 1){
printf("%d",x+y);
break;
}
}
return 0;
}
What could be the reason behind it?
scanf() returns the number of successful conversions it has performed for the input given.
With
scanf("%d%d", &x, &y)
you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.
If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:
#include <stdio.h>
int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%d\n", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != '\n');
}
}
return 0;
}
The more ideomatic way:
int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(\n\n", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != '\n');
}
printf("%d\n", x + y);

how to get program to accept only positive integer values in c

writing a program that will be finding min, max, avg of values entered by user. Having trouble writing something that will check to make sure there are only postive integers entered and produce an error message. heres my for statement that is reading the input so far:
for (int value = 0; value <= numofvals; ++value) {
printf("Value %d: %f\n", value, val_input);
scanf("%f", &val_input);
}
mind you I've been learning code for about 3 weeks and was just introduced to loops this week so my understanding is rudimentary at best!
First, don't use scanf. If stdin doesn't match what it expects it will leave it in the buffer and just keep rereading the same wrong input. It's very frustrating to debug.
const int max_values = 10;
for (int i = 0; i <= max_values; i++) {
int value;
if( scanf("%d", &value) == 1 ) {
printf("Got %d\n", value);
}
else {
fprintf(stderr, "I don't recognize that as a number.\n");
}
}
Watch what happens when you feed it something that isn't a number. It just keeps trying to read the bad line over and over again.
$ ./test
1
Got 1
2
Got 2
3
Got 3
foo
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
I don't recognize that as a number.
Instead, use fgets to reliably read the whole line and sscanf to parse it. %f is for floats, decimal numbers. Use %d to recognize only integers. Then check if it's positive.
#include <stdio.h>
int main() {
const size_t max_values = 10;
int values[max_values];
char buf[1024];
size_t i = 0;
while(
// Keep reading until we have enough values.
(i < max_values) &&
// Read the line, but stop if there's no more input.
(fgets(buf, sizeof(buf), stdin) != NULL)
) {
int value;
// Parse the line as an integer.
// If it doesn't parse, tell the user and skip to the next line.
if( sscanf(buf, "%d", &value) != 1 ) {
fprintf(stderr, "I don't recognize that as a number.\n");
continue;
}
// Check if it's a positive integer.
// If it isn't, tell the user and skip to the next line.
if( value < 0 ) {
fprintf(stderr, "Only positive integers, please.\n");
continue;
}
// We got this far, it must be a positive integer!
// Assign it and increment our position in the array.
values[i] = value;
i++;
}
// Print the array.
for( i = 0; i < max_values; i++ ) {
printf("%d\n", values[i]);
}
}
Note that because the user might input bad values we can't use a simple for loop. Instead we loop until either we've read enough valid values, or there's no more input.
Something easy like this may work for you:
int n;
int ret;
for (;;) {
ret = scanf("%d", &n);
if (ret == EOF)
break;
if (ret != 1) {
puts("Not an integer");
for (;;)
if (getchar() == '\n')
break;
continue;
}
if (n < 0) {
puts("Not a positive integer");
continue;
}
printf("Correct value %d\n", n);
/* Do your min/max/avg calculation */
}
/* Print your results here */
This is just an example and assumes you do not need to read floating point numbers and then check if they are integers, as well as a few other things. But for starters, it is simple and you can work on top of it.
To break out of the loop, you need to pass EOF (typically Ctrl+D in Linux/macOS terminals, Ctrl+Z in Windows ones).
An easy and portable solution
#include <limits.h>
#include <stdio.h>
int get_positive_number() {
char buff[1024];
int value, ch;
while (1) {
printf("Enter positive number: ");
if (fgets(buff, 1023, stdin) == NULL) {
printf("Incorrect Input\n");
// Portable way to empty input buffer
while ((ch = getchar()) != '\n' && ch != EOF)
;
continue;
}
if (sscanf(buff, "%d", &value) != 1 || value < 0) {
printf("Please enter a valid input\n");
} else {
break;
}
}
return value;
}
void solution() {
// Handling malformed input
// Memory Efficient (without using array to store values)
int n;
int min = INT_MAX;
int max = INT_MIN;
double avg = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
getc(stdin);
int value;
for (int i = 0; i < n; i++) {
value = get_positive_number();
if (value > 0) {
if (min > value) {
min = value;
}
if (max < value) {
max = value;
}
avg += value;
}
}
avg = avg / n;
printf("Min = %d\nMax = %d\nAverage = %lf\n", min, max, avg);
}
int main() {
solution();
return 0;
}
Output:
Enter number of elements: 3
Enter positive number: 1
Enter positive number: 2
Enter positive number: a
Please enter a valid input
Enter positive number: -1
Please enter a valid input
Enter positive number: 1
Min = 1
Max = 2
Average = 1.333333

Using a while loop to iterate through user input until specific number

I am trying to print out each integer on a new line, given integers separated by whitespace from a user input. It stops printing after a specific number, lets say 84. For example
The user input is
20 -4 84 8
How could i use a while loop to print these out as
20
-4
84
i know about scanf("%d %d %d %d", a, b, c, d), However the input size would be unknown, such that there could be only 3 numbers or 7 numbers. So far i have:
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
scanf("%d", &x);
while (x != 84) {
print("%d\n", x[i]);
i++;
}
}
Push the scanf into the while condition. Something like
while (scanf("%d", &x) != EOF && x != 84)
print("%d\n", x);
The basic concept should be of two-steps:
Read the number
Check and decide whether to print /continue.
A psuedo-code would look like
while ((ret =scanf(x)) && ret != EOF ){
if (x == VAL) break;
printf(x);
}
You have a few errors:
the array num does not exist;
the scanf must be repeated inside the while loop.
The corrected code is thw following:
#include <stdio.h>
int main(void)
{
int x;
while( 1 ){
scanf( "%d", &x );
print( "%d\n", x );
if( x==84 ) break;
}
}
You need to put scanf() inside the while loop in order to update every new input. No need for arrays if you want to print only the inputted values because you can print it after new input. Better do it with do-while loop.
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
do {
if ( scanf("%d", &x) < 0 )
break;
printf("%d\n", x);
} while (x != 84);
}

Scanf more values C

i need help with short Code in C. I must read floats on input line seperated with space and input is ended with float 0 or EOF.
How to do this if i dont know how many numbers or in input, or how it works and ask to EOF if i am reading just numbers and not chars?
Thanks for any response.
example of input in one line:
12 11 10 45 50 12 EOF
12 10 11 45 0
int main(void)
{
float num;
float sum = 0;
do{
scanf("%f", num);
sum += num;
} while(EOF || num == 0);
return 0;
}
From the man page of scanf -
scanf returns the number of items successfully matched and assigned
which can be fewer than provided for, or even zero in the event of an
early matching failure. The value EOF is returned if the end of input
is reached before either the first successful conversion or a matching
failure occurs.
This means that scanf will return EOF only when it encounters EOF as the first input when it is called because EOF must be preceded with a newline '\n' else it won't work (depending on the OS). You must also account for the matching failure scanf may encounter.
#include <stdio.h>
int main(void) {
float num;
float sum = 0;
int val;
while((val = scanf("%f", &num)) != EOF && val == 1) {
sum += num;
}
if(val == 0) {
printf("matching failure. input is not a float.\n");
}
else {
printf("end of input.\n");
}
return 0;
}
From scanf reference:
On success, the function returns the number of items of the argument
list successfully filled. This count can match the expected number of
items or be less (even zero) due to a matching failure, a reading
error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while
reading, the proper indicator is set (feof or ferror). And, if either
happens before any data could be successfully read, EOF is returned.
If an encoding error happens interpreting wide characters, the
function sets errno to EILSEQ.
So, you may rewrite your do-while loop to something like
int retval;
while((retval = scanf("%f", &num)) != EOF && retval > 0 && num != 0) {
sum += num;
}
if(retval == 0) {
printf("input read error.\n");
}
to match your constraints.
Also note you need to prefix your variable with & when passing it to scanf(), since the function expects a pointer to deal with (you need to pass variable address).
EDIT:
see this topic concerning EOF problems in Windows
You can re write your code like this
int main(void)
{
float num;
float sum = 0;
do
{
scanf("%f", &num);
sum += num;
} while((!feof(stdin)) && (num != 0));
printf("%f", sum);
return 0;
}
Here feof indicates end of input stream.
The following may be a slightly more robust way to do this:
#include <stdio.h>
#include <string.h>
int main(void) {
int sum=0;
int num;
char *p;
char buf[1000];
fgets(buf, 1000, stdin);
p = strtok(buf," ");
while(p!=NULL) {
if(sscanf(p, "%d", &num) == 1) sum+=num;
p = strtok(NULL, " ");
}
printf("the sum is %d\n", sum);
}
Test:
> testme
1 2 3 4 0
the sum is 10
> testme
1 2 3 4 ^D
the sum is 10
Note - you have to enter ctrl-D twice to get the desired effect when you are at the end of a line.
you can get your doubt clear by reading "C programming a modern approach by K N King"
This book provides proper clarification on this topic
Test the result of scanf() for 0, 1 or EOF.
Test the value scanned for 0.0.
int main(void) {
float num;
float sum = 0;
int cnt;
while ((cnt = scanf("%f", &num)) == 1) {
if (num == 0.0) break;
sum += num;
}
// cnt should be EOF, 0 or 1
if (cnt == 0) {
printf("Input is not a number\n");
}
else {
printf("Sum %f\n", sum);
}
return 0;
}
Although, in general, scanf() returns values EOF, 0, 1, ... "number of format specifiers", a value of 0 occurs rarely. Example input is "+".

Resources