This is probably me being blind from staring at the code for too long, but is my if statement not working properly? I am reading numbers from a file through stdin using scanf (I redirect the input when I run the program), and after reading a list of numbers, I am expecting a -999999999 to know that my list is over. I can read the -999... no problem, but when I try to use an if statement to to confirm the number of elements I read, it never goes in there. I'm using gcc on a linux environment.
Any comments?
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
long itemsA, itemsX, counter, reader;
int * A;
int * X;
//read in the size of A and X
scanf("%d %d", &itemsA, &itemsX); printf("\n a = %d, x = %d", itemsA, itemsX);
//allocate the arrays
A =(int *) malloc(itemsA*sizeof(int)); X = (int *) malloc(itemsX*sizeof(int));
//read in A and X
//counter=0;
for(counter=0; counter<itemsA; counter++) {
scanf("%d", &reader);
A[counter]=reader;
}
scanf("%d", &reader); printf("\nReader: %d", reader);
if(reader==-999999999)
{
printf("\nRead %d elements of A", counter);
}
Aucun commentaire:
Enregistrer un commentaire