I initialized a*, but I'm still getting the same problem...
I'm trying to read a file of polynomials where the first number tells you how many polynomials are in the file. From there I have to add, multiply, and evaluate them, but I have a general idea on how to do that. At the moment, I'm having problems with creating the array of pointers to separate polynomials.
<ex>
Code:
3
26x^5 + 5x^2 + 323x^3 + 50x^2 + 14x
8x^3 + 11x^2 + 34x + 4
21x^6 + 3x^5 + 4x + 4
Code:
#define MAXPOLY 100
#define CHAR 1000
#define NAME 50
int main(void) {
FILE *text;
int number, exp, p, k, go;
float coeff;
char again = 'Y', newFile = 'Y', operation, ch;
char fileName[NAME], *polynomial[MAXPOLY], charArray[MAXPOLY], *token, *a;
struct poly *first = NULL, *sum, *multiply1, *multiply2, *newLink, *pointers;
while(newFile == 'Y' || newFile == 'y') {
printf("\nEnter the file where the polynomials are found: \n");
fscanf(stdin, " %s", &fileName);
text = fopen(fileName, "r");
/* If file failed to open, print error statement and ask for a different file. */
if(text == NULL) {
printf("Error involved in opening file. Cannot open %s\n", fileName);
continue;
}
fscanf(text, " %d", &number);
/* Create an array of pointers. If error involved in allocating memory, terminate program. */
pointers = calloc(number, sizeof(struct poly));
if(pointers == NULL) {
printf("Error involved in allocating space. Cannot complete task.");
break;
}
a = &charArray[0];
ch = fgetc(text);
for(k = 0; k < number; k++) {
for(p = 0; p < MAXPOLY ; p++) {
ch = fgetc(text);
*(a+p) = ch;
if(ch == '\n') {
break;
}
}
if(k < number - 1) {
polynomial[k] = a;
a = calloc(1,MAXPOLY*sizeof(char));
if(a == NULL) {
printf("Error involved in allocating space. Cannot complete task.");
exit(EXIT_FAILURE);
}
}
}
return 0;
}