Code:
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main()
{
int counter = 0, num = 0;
int memory[N];
while(scanf("%d", &num) != EOF){
if (counter == N){
for(int i = 0; i< N-1; i++){
memory[i] = memory[i+1];
}
memory[N-1] = num;
continue;
}
memory[counter] = num;
counter++;
}
for(int i = 0;i < counter; i++){
printf("%d ", memory[i]);
}
return 0;
}
My while loop wouldn't terminate when I enter -1 as value, it just keeps registering more and more numbers into the array even the -1 and never reaches the second part of the code(the printf).
the only solution I could think of is adding another if statement but I feel like it's supposed to be working without it.
Code:
#include <stdio.h>
#include <stdlib.h>
#define N 5
int main()
{
int counter = 0, num = 0;
int memory[N];
while(scanf("%d", &num) != EOF){
if(num == EOF){
break;
}
if (counter == N){
for(int i = 0; i< N-1; i++){
memory[i] = memory[i+1];
}
memory[N-1] = num;
continue;
}
memory[counter] = num;
counter++;
}
for(int i = 0;i < counter; i++){
printf("%d ", memory[i]);
}
return 0;
}
If it's not supposed to be working without the second if statement may I recieve a small explanation to why it is like this?
Thanks alot in advance!