B
Brian Dude
Hello, I'm really at a loss as to what's going wrong with my code. I
have it posted below. How it appears is how I had it originally. I've
deleted all error-checking for simplicity:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
struct data2{
double x;
double y;
int index;
struct data2 *next;
}*first,*current,*hold;
do{
printf("How many pairs of data? ");
scanf("%d",&n);
fflush(stdin);
}while(n<2);
first=malloc(sizeof(struct data2));
first->index=0;
puts("\nEnter data pairs separated by spaces,\nentries separated by
ENTER:");
current=first;
while(1){
scanf("%lf %lf",¤t->x,¤t->y);
fflush(stdin);
current->index++;
current->next=malloc(sizeof(struct data2));
if(current->index >= n){
free(current->next);
current->next=NULL;
break;
}
current=current->next;
}
putchar('\n');
/*Quick verification...*/
current=first;
while(current){
printf("%lf : %lf\n",current->x,current->y);
current=current->next;
}
for(current=first;current!=NULL;current=hold){
hold=current->next;
free(current);
}
return 0;
}
Everything runs fine up until the first scanf() of the while loop. When
it reaches that statement, I get the following error message:
scanf : floating point formats not linked
Abnormal program termination
However, if I add the following declarations to the top:
double X,Y;
and replace the scanf() parameters with:
scanf("%lf %lf",&X,&Y);
current->x=X;
current->y=Y;
The numbers get entered into the list just fine... But incrementing
current->index goes awry and I get garbage numbers, so similarly, I have
to add to my declarations:
int i;
and work it into the while loop:
i++;
current->index=i;
and again it runs fine.
Another odd aspect to it. If I change only one of the parameters, say:
scanf("%lf %lf",&X,¤t->y);
current->x=X;
it runs fine (swapping the other two parameters works as well).
If there was no floating point library linked in, wouldn't I get an
error at link time?
TIA,
Brian Dude
have it posted below. How it appears is how I had it originally. I've
deleted all error-checking for simplicity:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
struct data2{
double x;
double y;
int index;
struct data2 *next;
}*first,*current,*hold;
do{
printf("How many pairs of data? ");
scanf("%d",&n);
fflush(stdin);
}while(n<2);
first=malloc(sizeof(struct data2));
first->index=0;
puts("\nEnter data pairs separated by spaces,\nentries separated by
ENTER:");
current=first;
while(1){
scanf("%lf %lf",¤t->x,¤t->y);
fflush(stdin);
current->index++;
current->next=malloc(sizeof(struct data2));
if(current->index >= n){
free(current->next);
current->next=NULL;
break;
}
current=current->next;
}
putchar('\n');
/*Quick verification...*/
current=first;
while(current){
printf("%lf : %lf\n",current->x,current->y);
current=current->next;
}
for(current=first;current!=NULL;current=hold){
hold=current->next;
free(current);
}
return 0;
}
Everything runs fine up until the first scanf() of the while loop. When
it reaches that statement, I get the following error message:
scanf : floating point formats not linked
Abnormal program termination
However, if I add the following declarations to the top:
double X,Y;
and replace the scanf() parameters with:
scanf("%lf %lf",&X,&Y);
current->x=X;
current->y=Y;
The numbers get entered into the list just fine... But incrementing
current->index goes awry and I get garbage numbers, so similarly, I have
to add to my declarations:
int i;
and work it into the while loop:
i++;
current->index=i;
and again it runs fine.
Another odd aspect to it. If I change only one of the parameters, say:
scanf("%lf %lf",&X,¤t->y);
current->x=X;
it runs fine (swapping the other two parameters works as well).
If there was no floating point library linked in, wouldn't I get an
error at link time?
TIA,
Brian Dude