1

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

1
  • After you fixed the attempt to use a reference parameter, you'll run into the problem that arrays are not assignable in t[i].type = GetNumber("...") and t[i].desc = .... Commented Mar 18, 2013 at 18:10

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.