Matt
Matt

Reputation: 482

Array of struct type,C error

So, I have this struct:

typedef struct {
    int day;
    int amount;
    char type[4],desc[20];
 }transaction;

And this function to populate a vector of type transaction ,declared in main:

void transact(transaction t[],int &n)
 {
    for(int i=0;i<n;i++)
    {
        t[i].day=GetNumber("Give the day:");
        t[i].amount=GetNumber("Give the amount of money:");
        t[i].type=GetNumber("Give the transaction type:");
        t[i].desc=GetNumber("Give the descripition:");
    }
 }

The error I get at the header of the function transact():

Multiple markers at this line
- Syntax error
- expected ';', ',' or ')' before '&' token

Upvotes: 1

Views: 192

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

C++ has references such as int &n; C does not.

Remove the &.

You're then going to have problems with assigning numbers to t[i].type and t[i].desc. They're strings, and you can't assign strings like that, and you should probably be using something like void GetString(const char *prompt, char *buffer, size_t buflen); to do the read and assignment:

GetString("Give the transaction type:", t[i].type, sizeof(t[i].type));

Upvotes: 3

interjay
interjay

Reputation: 110202

You are attempting to declare the n parameter as a reference (int &n). But references are a C++ feature and do not exist in C.

Since the function doesn't modify the value of n, just make it a normal parameter:

void transact(transaction t[],int n)

You also have errors later where you are attempting to assign an array:

t[i].type=GetNumber("Give the transaction type:");
t[i].desc=GetNumber("Give the descripition:");

Since GetNumber probably returns a number, it isn't clear what you are trying to do there.

Upvotes: 6

Related Questions