-1

I'm currently still practicing my c programming skills but there are so many errors here that I'm confuse on what is wrong and how to fix it. It's for a database program that I was practicing on.

It keeps showing:

new2.c:86: error: request for member ‘previousreading’ in something not a structure or union

and

new2.c:94: error: ‘Break’ undeclared (first use in this function)

#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;
Lundin
  • 195,001
  • 40
  • 254
  • 396
ZeroXR
  • 1

2 Answers2

3
  1. Put the typedef before main. typedefs must occure before you use them just as vaiables.
  2. Replace unsigned cust; by cust cust;. unsigned cust; is the same as unsigned int cust; and declares an unsigned integer, you want to declare a cust.
  3. Replace float charges; by float charge; in the typedef
  4. Replace Break; by break;. Case matters in C. Break is not Break, just as Int is not int.

Then it compiles.

Now if it it runs correctly or not is another story.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

There is not a single structure in your code, not in the form of a variable declaration nor as a type definition1, and you are treating cust which is simply an unsigned int as if it was a structure, perhaps you mean

struct {
    float previousreading;
    float currentreading;
    /* And so on */
} cust;

Also, there is no Break keyword in c, it's break, all lower case.

But,

  1. Don't do it, create a new struct so that you can use declare variables of type struct Costumer for example. Like at the end of your code, except that the compiler needs to know about it before using it, and the cust variable should have it's type.

  2. A char is not a string type, if you want a string you need an array of char, so char custname; is not going to work for the name string.

  3. Use meaningful names for your variables, and the members if your structure and the type name too. Like costumer instead of cust.

Additional NOTE

See Why while (!foef(file)) is always wrong. Your code will always attempt a read with fscanf() that will fail but it proceeds to print the data, it's very likely that your last row is printed twice once you make the code compile.

Instead, check the return value of fscanf(), if you don't know what it returns and don't fully understand it you can always read fscanf(3) documentation.


1At least not before you attempt to use it.

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97