Structure Arrays

Situation:

I am required to code a program that reads a customer's information from a file, "input.txt" to an array of structures. Then, the user should be given an opportunity to process the shipping bill for a specific customer recognized by his/her ID and write the results into an output file "output.txt".

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

  typedef struct
  { int custID;
    char name[15];
    char address[30];
    int numBikes;
    char bikeType;
    char cusRisk;
  } CUSTOMER;

    char n1[15];
    char a1[30];
    int bikes1;
    char biket1;
    char risk1;
    int id;
    int i = 0;
    int j;
    CUSTOMER cusArray[25];
    CUSTOMER *cusPtr;
    double total;

    system("clear");

    FILE *input;
    FILE *output;
    input= fopen("input.txt", "r");
    output= fopen("output.txt", "w");
    while (true) {
        fscanf( input, "%d", &id);
        fscanf( input, "%[^\n]", n1);
        fscanf( input, "%[^\n]", a1);
        fscanf( input, "%d", &bikes1);
        fscanf( input, "%c", &biket1);
        fscanf( input, "%c", &risk1);

        if (i < 25) {
            cusArray = setfield(cusArray, i, custID, id);
            cusArray = setfield(cusArray, i, name, n1);
            cusArray = setfield(cusArray, i, address, a1);
            cusArray = setfield(cusArray, i, numBikes, bikes1);
            cusArray = setfield(cusArray, i, bikeType, biket1);
            cusArray = setfield(cusArray, i, cusRisk, risk1);

        }
        i++;

        if (i >= 25) {
            break;
        }
    }

    fclose(input);

    printf("\nWhich Customer do you wish to write? (Enter ID Number)");
    scanf("%d", &j);
    cusPtr = cusArray + (j - 1);
    fprintf("\n********** Shipping Instructions **********");
    fprintf("\nTo:");
    fwrite(cusPtr->custID, sizeof(int), 4, output);
    fwrite(cusPtr->name, sizeof(char), 15, output);
    fwrite(cusPtr->address, sizeof(char), 30, output);

    if (cusPtr->bikeType == 'M') {
        total += (cusPtr->numBikes * 359.95);
        fwrite(cusPtr->numBikes, sizeof(int), 10, output);
    }

    if (cusPtr->bikeType == 'S') {
        total += (cusPtr->numBikes * 279.95);
        fwrite(cusPtr->numBikes, sizeof(int), 10, output);
    }

    if (cusPtr->cusRisk == 'N') {
        fprintf("\nby freight, and bill customer %10.2f", total);
    }
    if (cusPtr->cusRisk == 'Y') {
        fprintf("\nby freight, and COD customer %10.2f", total);
    }
    fprintf("\n*******************************************");



}


Problem:

One of the my usual gripes about the CS Major I am in is that the first two semesters of Computer Programming are done in Java, and then the third semester dumps you into C/C++. The class I am in automatically seems to assume I should know the syntax for it, yet I have tried looking up on google and other sources to see why this program does not work. Compilation of this program results in sheer madness. I would appreciate any and all assistance with this program.

-Curon
Last edited on
1
2
3
4
5
6
cusArray = setfield(cusArray, i, custID, id);
cusArray = setfield(cusArray, i, name, n1);
cusArray = setfield(cusArray, i, address, a1);
cusArray = setfield(cusArray, i, numBikes, bikes1);
cusArray = setfield(cusArray, i, bikeType, biket1);
cusArray = setfield(cusArray, i, cusRisk, risk1);


Where is setfield function defined ? Where are variables custID,name,address,numBikes,bikeType,cusRisk defined ?

 
fprintf("\nby freight, and bill customer %10.2f", total);


fprintf is a C built-in function and your arguments are wrong. I believe the first parameter is a FILE*

man fprintf to see what are the arguments needed.

Topic archived. No new replies allowed.