我目前仍在练习我的c编程技巧,但这里有很多错误,我对错误以及如何修复它感到困惑。它是我正在练习的数据库程序的。
它一直显示:
new2.c:86:错误:在非结构或联合的内容中请求成员'previousreading'
和
new2.c:94:错误:'中断'未声明(首次使用此功能)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int custid;
char custname;
float currentreading;
float previousreading;
double charge;
int choice;
unsigned cust;
int revenue, meterdifference, BILL;
printf("----------------------------------\n");
printf("Electricity Management System\n");
printf("----------------------------------\n");
printf("\n1. Record Usage");
printf("\n2. Add Customer");
printf("\n3. Edit Customer");
printf("\n4. Delete Customer");
printf("\n5. Show Customer");
printf("\n6. Show Total monthly income");
printf("\n7. Exit");
scanf("%d",&choice);
if(choice >=1 || choice <=7)
{
switch(choice)
{
case 1: //Record Usage
printf("Enter Customer ID\n");
FILE *cfPtr;
if ((cfPtr = fopen("customer.txt", "r"))== NULL)
puts("This file could not be opened");
else
{
puts("Enter the customer ID, name.");
scanf("%d%29s", &cust.custid, cust.custname);
puts("Enter the current reading in kWh");
scanf("%d", cust.currentreading);
if(cust.currentreading < cust.previousreading)
puts("Input invalid");
else
{
if (cust.currentreading>=200)
{
cust.charge = (cust.currentreading - cust.previousreading)*21.80;
printf("\nThe charge is RM%f\n", &cust.charge);
}
else
{
if (cust.currentreading>=300)
{
cust.charge= ((cust.currentreading - cust.previousreading)*33.40)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (cust.currentreading>=600)
{
cust.charge= ((cust.currentreading - cust.previousreading)*51.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
if (currentreading>=900)
{
cust.charge = ((cust.currentreading - cust.previousreading)*54.60)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
else
{
cust.charge = ((cust.currentreading - cust.previousreading)*57.10)+21.80;
printf("\nThe charge is RM%f", &cust.charge);
}
}
}
}
}
}
Break;
case2: //Add Customer
puts("This option allows user to add new customer");
printf("Enter Customer ID and name.");
scanf("%d%c", &cust.custid, cust.custname);
puts("To return to menu");
Break;
case 3: //Edit Customer
puts( "This option allows user to edit customer info");
Break;
case 4: //delete customer
puts( "This option allows user to delete customer");
Break;
case 5: //Show Customer
printf("To show customer information\n");
FILE*tPtr;
char custid[100],custname[100];
int previousreading,currentreading;
double charge;
printf("\n Show Customer\n");
if((tPtr= fopen("customer.txt","r"))==NULL){
puts("File not found");
}
else{
printf("%-15s%-25s%-20s%-15s%-15s\n","ID","Name","Previous Reading","Current Reading","Charges");
while(!feof(tPtr)){
fscanf(tPtr,"%[^;];%[^;];%d;%d;%lf",cust.custid,cust.custname,&cust.previousreading,&cust.currentreading,&cust.charge);
printf("%s\t\t%-25s%-20d%-15d%-15.2lf",cust.custid,cust.custname,cust.previousreading,cust.currentreading,cust.charge);
}
fclose(tPtr);
}
printf("\n\n");
Break;
case 6: //Show total income(monthly)
puts("To show monthyly income");
printf("total usagekWh, meterdifference");
printf("%-15s%-35.2d\n", "Total UsagekWh","meterdifference");
scanf("%-16dtotal usage(kWh)%-24d: %.2f",&meterdifference);
printf("%-13dtotal revenue%-24d: %.2f",BILL);
revenue=BILL;
printf("revenue is %.2f", BILL);
Break;
case 7: //Exit
Break;
}
}
else
printf("\nError. Number not in choices.");
return 0;
}
typedef struct{
int custid[50];
char custname[100];
int previousreading;
int currentreading;
float charges;
}cust;
答案 0 :(得分:3)
c
放在 typedef
之前。 main
必须在之前发生,你就像使用vaiables一样。typedef
替换为unsigned cust;
。 cust cust;
与unsigned cust;
相同,并声明无符号整数,您要声明unsigned int cust;
。cust
替换为float charges;
float charge;
typedef
替换为Break;
。 C. break;
中的案例事项不是Break
,正如Break
不是Int
。然后编译。
现在,如果它正确运行或不是另一个故事。
答案 1 :(得分:0)
您的代码中没有单一结构,不是以变量声明的形式,也不是类型定义 1 ,而您正在处理cust
,这只是一个无符号{ {1}}好像它是一个结构,也许你的意思是
int
此外,c中没有struct {
float previousreading;
float currentreading;
/* And so on */
} cust;
个关键字,Break
,全部为小写。
但是,
不要这样做,创建一个新的break
,以便您可以使用类型为struct
的声明变量。就像代码末尾一样,除了编译器在使用之前需要知道它,struct Costumer
变量应该有它的类型。
cust
不是字符串类型,如果您需要字符串,则需要char
数组,因此char
不适用于名称字符串。
为变量使用有意义的名称,如果您的结构和类型名称也使用成员。与char custname;
代替costumer
。
附加说明
见Why while (!foef(file))
is always wrong。您的代码将始终尝试使用cust
进行读取,但会继续读取,但会继续打印数据,一旦您编译代码,很可能会将您的最后一行打印两次。
相反,请检查fscanf()
的返回值,如果您不知道它返回的内容并且完全不了解它,则可以随时阅读fscanf(3)文档。
1 至少在您尝试使用它之前。