I have a doubt, which ruins all my programs. I have Linux Mint and edit in CodeBlocks. When I run my programs, for example: `#include <stdlib.h> #include <stdio.h>
int main()
{
int n;
printf("Introduzca el número de elementos en el arreglo:\n");
scanf("%d\n",&n);
int i,x[n];
printf("Introduzca los %d elementos:\n",n);
for(i=0; i<n; i++)
scanf("%d\n",&x[i]);
printf("Los elementos del arreglo son:\n");
for(i=0; i<n; i++)
printf("%d\t",x[i]);
printf("\n");
return 0;
}
` My output: Enter the number of elements in the array: 6
two
Enter the 6 elements: 9 1 3 5 6 7 The elements of the array are: 2 9 1 3 5 6
This is weird because when it tells me to enter the array elements, I typed 6 and ENTER and nothing happened; then add the 2, for example (it could have been another number) and I gave it ENTER again and now if it was put: Enter the 6 elements. I put the 6 elements (from 9 to 7) and it printed from 2 (which was the number that allowed me to advance to the next step of the program) to 6, ignoring 7! Could you help me with this. It is very important to me because it happens to me in all my programs.
Your error is in the function
scanf()
, since you are inserting a line break with\n
. This type of "functionality" should not be implemented in the data input functions (scanf()
), but in the output functions (printf()
). Otherwise you are correct.The code would look like this: