質問

I'm having trouble using decision statements with type def struct variables, also i cant declare the variables to equal zero anywhere.

Here is the struct

typedef struct{
    int wallet;
    int account;
}MONEY;

It was declared in main as MONEY money; (but for the function im having trouble with its *m)

When i try to do something like this

*m.wallet = *m.wallet - depositAmmount;

or this

*m.wallet = 0;

It gives me a syntax error saying expression must have a class type. What do i do? Inside the function I tried declaring int *m.wallet = 0;and even DATE *m.wallet; to no avail

below is the whole function

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>
#include <math.h>
#define clear system("clear") 
#define pause system("pause") 
void banking(MONEY *m){
    int userChoice = 0;
    int withdrawAmmount = 0;
    int depositAmmount = 0;
    do{
        userChoice = 0;
        withdrawAmmount = 0;
        depositAmmount = 0;
        displayBankMenu(*m);
        scanf("%i", &userChoice);
        clear;

        switch (userChoice) {
            case 1:
                    printf("How much would you like to Deposit?: $");
                    scanf("%i", &depositAmmount);
                    if(depositAmmount <= *m.wallet){
                        *m.wallet = *m.wallet - depositAmmount;
                        *m.account = *m.account + depositAmmount;
                    }else{
                        printf("You do not have sufficient funds for this transaction.");
                        pause;
                    }

                    break;
            case 2:                 
                    printf("How much would you like to withdraw?: $");
                    scanf("%i", &withdrawAmmount);
                    if(withdrawAmmount <= *m.account){
                        *m.account = *m.account - withdrawAmmount;
                        *m.wallet = *m.wallet + withdrawAmmount;
                    } else{
                        printf("You do not have sufficient funds for this transaction.");
                        pause;
                    }

                    userChoice = 0;

                    break;
        }
    }while(userChoice != 3);
} //end banking
役に立ちましたか?

解決

To access a member of a pointer to a structure, use the -> operator.

m->wallet = m->wallet - depositAmmount;
m->wallet = 0;

The . operator has higher precedence than the * dereference, so you need to use parentheses for that expression to obtain the structure member.

(*m).wallet = (*m).wallet - depositAmmount;
(*m).wallet = 0;

他のヒント

Structure member operator . has higher precedence than the dereference operator *. So the following statement

*m.wallet = 0;

is the same as

*(m.wallet) = 0;

It means get the member wallet of the struct m and then dereference it, assuming it's a pointer. But m is a pointer to the structure, not the structure itself. So you should first dereference the pointer to get the structure and then use . operator to get the wallet member.

(*m).wallet = 0;

You can also use the more convenient arrow operator -> as

m->wallet = 0;

They are the same.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top