预期'('用于功能风格或类型结构

时间:2015-03-26 23:01:48

标签: c compiler-errors call

所以我试图在我的主要功能中调用我的开关功能中的一个功能,它告诉我"期待'('用于功能风格或类型施法。"我在这里做错了什么导致了这个错误?我似乎无法解决这个问题。谢谢。

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 6
#include <string.h>

typedef struct
{
    char name[100];
    unsigned int accountId;
    float accountBalance;
    int yearJoined;
} BankingInfo;


void createTestData(BankingInfo inventory[]){
    strcpy(inventory[0].name, "Alex");
    inventory[0].accountId = 1001;
    inventory[0].accountBalance = 2390;
    inventory[0].yearJoined = 2001;

    strcpy(inventory[1].name, "Bill");
    inventory[1].accountId = 1003;
    inventory[1].accountBalance = 10000;
    inventory[1].yearJoined = 2012;

    strcpy(inventory[2].name, "Craig");
    inventory[2].accountId = 1004;
    inventory[2].accountBalance = 500;
    inventory[2].yearJoined = 1994;

    strcpy(inventory[3].name, "David");
    inventory[3].accountId = 1008;
    inventory[3].accountBalance = 5836;
    inventory[3].yearJoined = 2013;

    strcpy(inventory[4].name, "Eric");
    inventory[4].accountId = 1016;
    inventory[4].accountBalance = 90;
    inventory[4].yearJoined = 1999;

    strcpy(inventory[5].name, "Felix");
    inventory[5].accountId = 1032;
    inventory[5].accountBalance = 5703;
    inventory[5].yearJoined = 2003;
}

void outputTable(BankingInfo inventory[], int size){
    int i;
    printf("Names\tID\t\tAccount Balance\t\tYear Joined\n");

    for (i = 0; i < ARRAY_SIZE; i++) {
        printf("%s\t%u\t   %.2lf\t\t\t%i\n",
               inventory[i].name,inventory[i].accountId,
               inventory[i].accountBalance, inventory[i].yearJoined);
    }
}

void searchById(BankingInfo inventory[], int size, int id){
    unsigned int enteredId;
    int i;
    printf("Enter ID number:  ");
    scanf("%u", &enteredId);
    for (i = 0; i < ARRAY_SIZE; i++) {
        if (inventory[i].accountId == enteredId) {
            printf(inventory[i].name,inventory[i].accountId,
                   inventory[i].accountBalance, inventory[i].yearJoined);
        }
    }

}

void searchByYear(BankingInfo inventory[], int size, int year){
    unsigned int enteredYear;
    int i;
    printf("Enter year joined:  ");
    scanf("%u", &enteredYear);
    for (i = 0; i < ARRAY_SIZE; i++) {
        if (inventory[i].yearJoined == enteredYear) {
            printf(inventory[i].name,inventory[i].accountId,
                   inventory[i].accountBalance, inventory[i].yearJoined);
        }
    }

}

int main(void){

    int choice;
    BankingInfo inventory[ARRAY_SIZE];
    createTestData(inventory);
    outputTable(inventory, ARRAY_SIZE);

    do{
        printf("Enter 1 to search by ID\n");
        printf("Enter 2 to search by year joined\n");
        printf("Enter 3 to finish\n");

        switch (choice) {
            case 1:
                searchById(inventory, ARRAY_SIZE, int id);
                break;

            case 2:
                searchByYear(inventory, ARRAY_SIZE, int year);
                break;

            case 3:
                break;

            default:
                printf("Not a valid choice\n");
                break;
        }
    }while (choice != 3);

    return 1;
} 

1 个答案:

答案 0 :(得分:1)

你在函数调用参数中有一个类型说明符,像这样两次

searchById(inventory, ARRAY_SIZE, int id);
/*                                 ^ this ???? */

删除它

searchById(inventory, ARRAY_SIZE, id);

然后声明并初始化idyear

我修复了你的程序,希望你能理解我的所作所为

#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 6
#include <string.h>

typedef struct
{
    char name[100];
    unsigned int accountId;
    float accountBalance;
    int yearJoined;
} BankingInfo;


void createTestData(BankingInfo inventory[])
{
    strcpy(inventory[0].name, "Alex");
    inventory[0].accountId = 1001;
    inventory[0].accountBalance = 2390;
    inventory[0].yearJoined = 2001;

    strcpy(inventory[1].name, "Bill");
    inventory[1].accountId = 1003;
    inventory[1].accountBalance = 10000;
    inventory[1].yearJoined = 2012;

    strcpy(inventory[2].name, "Craig");
    inventory[2].accountId = 1004;
    inventory[2].accountBalance = 500;
    inventory[2].yearJoined = 1994;

    strcpy(inventory[3].name, "David");
    inventory[3].accountId = 1008;
    inventory[3].accountBalance = 5836;
    inventory[3].yearJoined = 2013;

    strcpy(inventory[4].name, "Eric");
    inventory[4].accountId = 1016;
    inventory[4].accountBalance = 90;
    inventory[4].yearJoined = 1999;

    strcpy(inventory[5].name, "Felix");
    inventory[5].accountId = 1032;
    inventory[5].accountBalance = 5703;
    inventory[5].yearJoined = 2003;
}

void outputTable(BankingInfo inventory[], int size)
{
    int i;

    printf("Names\tID\t\tAccount Balance\t\tYear Joined\n");
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        printf("%s\t%u\t\t%5.2lf\t\t%d\n",
            inventory[i].name,
            inventory[i].accountId,
            inventory[i].accountBalance, 
            inventory[i].yearJoined
        );
    }
}

void searchById(BankingInfo inventory[], int size, int id)
{
    int i;
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        if (inventory[i].accountId == id)
        {
            printf("%s\t%u\t\t%5.2lf\t\t%d\n",
                inventory[i].name,
                inventory[i].accountId,
                inventory[i].accountBalance,
                inventory[i].yearJoined
            );
        }
    }

}

void searchByYear(BankingInfo inventory[], int size, int year){
    unsigned int enteredYear;
    int i;
    printf("Enter year joined:  ");
    scanf("%u", &enteredYear);
    for (i = 0; i < ARRAY_SIZE; i++)
    {
        if (inventory[i].yearJoined == enteredYear)
        {
            printf("%s\t%u\t\t%5.2lf\t\t%d\n",
                inventory[i].name,
                inventory[i].accountId,
                inventory[i].accountBalance,
                inventory[i].yearJoined
            );
        }
    }

}

int main(void)
{
    char        choice;
    BankingInfo inventory[ARRAY_SIZE];

    createTestData(inventory);
    outputTable(inventory, ARRAY_SIZE);
    do {
        int  id;
        int  year;

        printf("\n");
        printf("\t1 Search by ID\n");
        printf("\t2 Search by year joined\n");
        printf("\t3 Finish\n\n");

        printf("Please input your choice > ");
        scanf(" %c", &choice);
        switch (choice)
        {
            case '1':
                printf("Enter item id > ");
                if (scanf("%d", &id) == 1)
                    searchById(inventory, ARRAY_SIZE, id);
                else
                    printf("invalid input\n");
                break;
            case '2':
                printf("Enter item year > ");
                if (scanf("%d", &year) == 1)
                    searchByYear(inventory, ARRAY_SIZE, year);
                else
                    printf("invalid input\n");
                break;
            case '3':
                break;
            default:
                printf("Not a valid choice\n");
                break;
        }
    } while (choice != '3');

    return 0;
}