scanf requires more inputs than requested [duplicate] - c

This question already has answers here:
Store data in array from input [duplicate]
(2 answers)
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 9 years ago.
I'm trying to get 5 float values from the user using scanf, the problem is the user is required to input 6 values for the program to complete.
Although I know I shouldn't use scanf, it bothers me that there's something I can't grasp about it. Any insights, any advice on how to fix it whilst using scanf?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0 , j = 0;
char buf[128] = {0};
float numbers[5] = {0.0};
float keep = 0.0;
printf("Please input 5 numbers : \n");
for (i = 0; i < 5; i++)
{
scanf("%f\n", &numbers[i]);
}
printf("Done!");
Thanks,
MIIJ

you must remove \n in scanf() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0 , j = 0;
char buf[128] = {0};
float numbers[5] = {0.0};
float keep = 0.0;
printf("Please input 5 numbers : \n");
for (i = 0; i < 5; i++)
{
scanf("%f", &numbers[i]);
printf("number %i is %f \n",i,numbers[i]);
}
printf("Done!");
return 0;
}

scanf("%f\n", &numbers[i]); should be scanf("%f", &numbers[i]);

Related

Unexpected output when using scanf("%d\n") [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 3 years ago.
The output I expect after reading 3 times is the sum of those three numbers,
but when reading the numbers there is a problem with the printing.
for (i = 0; i < TAMANIO; i++) {
scanf("%d\n",&ar[i]);
printf("numero: %d\n",ar[i]); //print numbers
}
If you need full code.
#include <stdio.h>
#define TAMANIO 3
//prototipo de funciones
void pri();
int pro(int a, int b, int c);
int main () {
pri(); //Ask for numbers
int i; //counter
int ar[TAMANIO];
for (i = 0; i < TAMANIO; i++) {
scanf("%d\n",&ar[i]);
printf("numero: %d\n",ar[i]); //print numbers
}
printf("suma de numeros: %d\n", pro(ar[0],ar[1],ar[2])); //send number to function and print
return 0;
}
void pri(){
//write numbers
printf("Escriba los 3 numeros a ser operados : \n");
}
int pro(int a, int b, int c) {
int sum;
sum = a + b + c;
return sum;
}
I expect when reading:
"enter numbers"
1
number : 1
2
number : 2
3
number : 3
sum : 6
[ More Scanf() info ] What is the effect of trailing white space in a scanf() format string?
You could try changing scanf("%d\n",&ar[i]); to scanf("%d",&ar[i]);, because it will try to read newline break too in a loop.
Simply remove \n from the scan
You need to remove the \n from scanf.
for (i = 0; i < TAMANIO; i++) {
scanf("%d",&ar[i]);
printf("numero: %d\n",ar[i]); //print numbers(here is the problem)
}

C scanf ignore second variable in loop [duplicate]

This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 3 years ago.
int main()
{
float T[100];
float *pt=T;
float suma = 0, srednia, zmienna;
int rozmiar;
printf("How many numbers would you like to put in: ");
scanf(" %d", &rozmiar);
int dzielnik = rozmiar;
printf("\n Enter the number: \n");
for(int i = 0;i<rozmiar;i++)
{
printf("\n i = %d", i );
scanf("%99f\n", &zmienna);
*(pt+i) = zmienna;
}
return 0;
}
This is my code. The idea is simple. I have an array; I want to scan how many numbers I want to put into the array and then put numbers into array. I don't know why but scanf ignores the second variable that I put in array.
If I put "2" in first scanf, program wants 3 variables from me.
My output should be like this:
How many numbers would you like to put in: 2
Enter the number:
i = 0
2 (my number)
i=1
3 (my number)
but it's actually like this:
How many numbers would you like to put in: 2
Enter the number:
i = 0
1 (my number)
2 (my number)
i = 1
3 (my number)
The specific problem you were having is with scanf: putting a \n is generally a bad idea because entering a newline is generally how you send off a line of characters to the program. Use '\n' liberally in printf and "never" with scanf.
Here's my modified version. I removed the pointer shenanigans because indexing into the array is simpler and better, and also initialized the array which you should always do:
#include <stdio.h>
int main() {
float T[100] = {0}; // Used to not be initialized.
float suma = 0, zmienna;
int rozmiar;
printf("How many numbers would you like to put in: ");
scanf("%d", &rozmiar);
printf("\n Enter the number: \n");
for (int i = 0; i<rozmiar; i++) {
printf("\n i = %d", i);
scanf("%f", &zmienna);
T[i] = zmienna;
}
return 0;
}

Why do I get a segmentation error here in my code? [duplicate]

This question already has answers here:
Why does scanf() need & operator (address-of) in some cases, and not others? [duplicate]
(5 answers)
Ampersand(&) and scanf in C?
(5 answers)
errors and warning message when using pointer
(3 answers)
Closed 5 years ago.
It's an extremely simple code, when I run it, it lets me enter the int, but when I enter it, the program crashes. I tried putting a printf right after the scanf to check if the program reads the int correctly, it seems it doesn't. I can't output the integer that I input.
#include <stdio.h>
#include <math.h>
void coolfunction(int x, int *y)
{
*y = exp(x);
}
int main()
{
int n;
double result = 0;
printf("Enter an integer: ");
scanf("%d", n);
coolfunction(n, &result);
printf("\n e^%d = %lf \n", n, result);
return 0;
}
Here's a version using a double parameter:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void coolfunction(int x, double *y)
{
*y = exp(x);
}
int main()
{
int n = 0, scanned = 0;
double result = 0;
printf("Enter an integer: ");
scanned = scanf("%d", &n);
if (scanned < 1) {
fprintf(stderr, "value entered not a valid integer\n");
exit(EXIT_FAILURE);
}
coolfunction(n, &result);
printf("\n e^%d = %g \n", n, result);
return 0;
}
You also missed the & in your scanf call: the function needs to know the address of your variable, not its value.

Scanf is being ignored [duplicate]

This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 7 years ago.
#include <stdio.h>
#define length 20
main()
{
float x;
int y;
float array1[length], array2[length], array3[length];
float ray[length];
int size1 = insert(array1, length);
printf("enter number: ");
scanf("%d", &y);
int size2 = insert(array2, length);
int size3 = insert(array3, length);
}
int insert(float a[], int size)
{
int n = 0;
while(n<size && scanf("%f\n", &a[n]) == 1)
{
printf("you entered: ");
printf("%2.1f\n", a[n]);
n++;
}
return n;
}
When I run the program, it executes the first insert okay, but the next time function is called, scanf() seems to be ignored completely. I tried putting it right after where insert is done, but that's ignored as well.
Use %*c in scanf to consume the newlines along with space around %d in the scanf in main(). I tested the below code on MingW and it seem to work. The '\n' in your scanf is being consumed making it scanf() return while the '\n' at the press of enter key still remains in IO buffer to be consumed by scanf() again; hence the weird behaviour.
#include <stdio.h>
#include <stdlib.h>
#define length 20
int insert(float *a, int size)
{
int n = 0;
while(n<size && scanf("%f%*c", &a[n]))
{
printf("you entered: ");
printf("%2.1f\n", a[n]);
n++;
}
return n;
}
int main(int argc, char* argv[])
{
float x;
int y;
float array1[length], array2[length], array3[length];
float ray[length];
int size1 = insert(array1, length);
printf("enter number: ");
scanf("%d", &y);
int size2 = insert(array2, length);
int size3 = insert(array3, length);
return 0;
}
In the scanf format string, change "%f\n" to "%f". The \n in a scanf format string does not mean "wait for newline".
You do not need to worry about waiting for newline, because the only format specifiers you use are %f and %d, which both discard any leading whitespace.

Check for Character instead of Integer [duplicate]

This question already has answers here:
Check if a value from scanf is a number?
(2 answers)
Closed 9 years ago.
my program adds numbers entered by the user. It runs and works great until a character is entered instead of an integer. Is there a simple way to make sure only integers are entered from the keyboard?
Here is my code.
#include<stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int n, sum = 0, i, TotalOfNumbers;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (i = 1; i <= n; i++)
{
scanf("%d",&TotalOfNumbers);
sum = sum + TotalOfNumbers;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
You need to check the return value of scanf. If the input was a valid number, it will return 1. If the input was not a valid number, it will return something else. Here is your code modified to put the checks in.
#include<stdio.h>
#include <stdlib.h>
int get_number()
{
int num;
int ret;
ret = scanf("%d", &num);
if (ret != 1) {
printf("bad number\n");
exit(EXIT_FAILURE);
}
return num;
}
int main(int argc, char **argv)
{
int n, sum = 0, i, TotalOfNumbers;
printf("Enter the number of integers you want to add\n");
n = get_number();
printf("Enter %d integers\n",n);
for (i = 1; i <= n; i++)
{
TotalOfNumbers = get_number();
sum = sum + TotalOfNumbers;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
Check the ferror state on the input stream
scanf("%d",&TotalOfNumbers);
if(!ferror(stdin)){
sum = sum + TotalOfNumbers;
}
In addition to posted answer, there options not general as posted, but quicker.
First if you want to skip some final set of characters.In following example all letters,! and + will be skiped
int n;
scanf("%*[a-zA-Z!+]%d",&n);
printf("\n%d",n);
for input
weweqewqQQWWW!!!!+++3332
the output is
3332
Next option is to use buffer wich allowed to read everything untill number is met, and then read the number. The disadvantage is that buffer size is limited
char buf[25];
int n;
scanf("%[^0-9]%d",buf,&n);
printf("\n%d",n);
For input
fgfuf#$#^^#^##4565
Output
4565

Resources