gpt4 book ai didi

c - 对于函数样式或类型构造,应为 '('

转载 作者:行者123 更新时间:2023-11-30 19:16:50 29 4
gpt4 key购买 nike

因此,我尝试在 switch 函数中调用主函数中的函数,它告诉我“函数样式或类型构造需要 '('”。我在这里做错了什么导致此错误?我可以似乎不明白。谢谢。

#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;
}

最佳答案

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

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

关于c - 对于函数样式或类型构造,应为 '(',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290419/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com