Electricity Bill generator using C++

Electricity Bill generator using C++ (With Source Code)

Electricity Bill generator using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create an Electricity Bill generator using C++ (With Source Code).

The Electricity Bill generator is centered on the idea of producing an individual’s electricity bill structures and is made to keep the records about the bills of the customers in an efficient manner. This project consists of  an Admin login feature. The admin can use its functions, which include adding new customers, generating the bill, display bill and delete bill. As a result, this project comprises only the basic functionality of generating Electricity bill of individuals.

Electricity Bill generator using C++

In terms of the system, this project is concerned with the creation of power bills for consumers by inputting their information and searching the database. The user must first log in to the system. The login system requires a username and password, and only an administrator can access the program. The administrator’s username is “admin”, and the password is “12345”. The administrator may then add a new client and produce their bill as needed. If the bill contains an error, the administrator can even eliminate it from the system. Customers’ and their bills’ information is saved in the database as a text file.

Simple Clinic Management System using C++ (With Source Code)

This program uses file handling to store and retrieve data. It has two text files:

  1. “cust.txt”
  2. “bill1.txt”.

The “cust.txt” file stores the customer details, while the “bill1.txt” file stores the bill details.

Overview of the UI:

  1. Add new customer
  2. Bill Generation
  3. Display bill
  4. Delete bill
  5. EXIT

The admin will select one of the options given above to proceed further. The admin at first will have to choose option 1 to add new customers in order to use option 2,3 and 4 to generate their bill, display bill or delete them respectively.

Electricity Bill generator

Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct customer
 {
   char c_name[20];
   int c_id;
   float price;
 };
 
struct bill
 { 
   int b_id;
   int cid;
   char name[20];
   float price;	
   int unit;
 };
 
 int customer_id()
  {
   int a;
   FILE *fp;
   fp=fopen("cus.txt","r");
    if(fp==NULL) 
      a=1;
    else
      {
       fscanf(fp,"%d",&a);
       a++;
       return a;
      }
   fclose(fp);
   
   fp=fopen("cus.txt","w");
   fprintf(fp,"%d",a);
   fclose(fp);
   
  } 
  
  int bill_id()
  {
   int y;
   FILE *fp;
   fp=fopen("bi.txt","r");
    if(fp==NULL) 
      y=1;
    else
      {
       fscanf(fp,"%d",&y);
       y++;
      }
   fclose(fp);
   
   fp=fopen("bi.txt","w");
   fprintf(fp,"%d",y);
   fclose(fp);
   return y;
  } 
   
 void add_customer()
  {
   FILE *ptr;
   int i;
   struct customer c;
   ptr=fopen("cust.txt","a");
     fflush(stdin);
   printf("\n Enter customer details :- \n Name  -  ");
   gets(c.c_name);
     fflush(stdin);
 
   printf("\n Price of each unit  -  ");
   scanf("%f",&c.price);
   c.c_id=customer_id();
    
       printf("\n Record added successfully.  ");
       printf("\n Your customer id is   %d",c.c_id); 	
    
   fwrite(&c,sizeof(c),1,ptr);
   fclose(ptr);   
 } 
  
 void generate_bill()
  {
   int find,flag=0;
   FILE *ptr,*ptr1;
   struct customer c;
   struct bill b;
   printf("\n Enter your customer id.  ");
   scanf("%d",&find);
    
   ptr=fopen("cust.txt","r");
    while(!feof(ptr))
     {
       fread(&c,sizeof(c),1,ptr);
       if(c.c_id==find)
        { flag=1;
          ptr1=fopen("bill1.txt","a");
          b.b_id=bill_id();
          strcpy(b.name,c.c_name);
          b.price=c.price;
          b.cid=c.c_id;
          printf("\n Enter number of unit consumed in this month.  ");
          scanf("%d",&b.unit);
          fflush(stdin);
          printf("\n Bill has been generated successfully.   ");
          printf("\n Your bill id is   %d",b.b_id);
          fwrite(&b,sizeof(b),1,ptr1);
          fclose(ptr1);
          break;
        }	
     }
   if(flag==0)
      printf("\n Error! No such customer with id no.  %d exist.",find);
   fclose(ptr);	
  }
  
  void display_bill()
 { 
   int flag=0,billid,custid;
   FILE *ptr1;
   struct bill b;
   ptr1=fopen("bill1.txt","r");
   printf("\n Enter your customer id.  ");
   scanf("%d",&custid);
   fflush(stdin);
   printf("\n Enter bill id. whose bill you want to display.  ");
   scanf("%d",&billid);
    while(!feof(ptr1))
     {  fflush(stdin);
     
       fread(&b,sizeof(b),1,ptr1);
        if((b.b_id==billid)&&(b.cid==custid))
         {
           flag++;
           
           printf("\n   Electricity Bill");
           printf("\n   Bill id        :- %d",b.b_id);
           printf("\n   Name           :- %s",b.name);
           printf("\n   Customer id    :-  %d",b.cid);
           printf("\n   Price per unit :- %f",b.price);
           printf("\n   Unit consumed  :- %d",b.unit);
           printf("\n   Total amount   :- %f",b.price*b.unit);
           break;
         }
     } 
  if(flag==0)
      printf("\n Error! No such customer id no.  %d  OR bill no. %d  exist.",custid,billid);
  fclose(ptr1);    
}
  
  void delete_bill()
   {
   
    int flag=0,billid,custid;
    FILE *ptr,*ptr1;
    struct bill b;
 
    ptr=fopen("bill1.txt","r");
    ptr1=fopen("temp.txt","w");	
    printf("\n Enter bill id. whose bill you want to delete.  ");
    scanf("%d",&billid);
     while(!feof(ptr))
      {
       fread(&b,sizeof(b),1,ptr);
        if(b.b_id==billid)
         {
           printf("\n Bill with id no.  %d DELETED successfully.",b.b_id);
           flag=1;
         }
        else
         fwrite(&b,sizeof(b),1,ptr1);
      }
   
    fclose(ptr);  
    fclose(ptr1);
     if(flag==0)
      printf("\n Error! No such bill with id no.  %d  exist.",billid);
    remove("bill1.txt");
    rename("temp.txt","bill1.txt");
   }
  
 main()
  { 
  
char username[20];
    char password[20];
    printf("please enter the username:");
    scanf("%s",username);
    printf("please enter the password: ");
    scanf("%s",password);
    if(strcmp(username,"admin")==0)
    {
        if(strcmp(password,"12345")==0)
        {
            printf("\nwelcome _____login successfull");
        }
        else
        {
            printf("\n invalid password");
            exit(0);
        }
    }
    else
    {
        printf("\n username is invalid");
        exit(0);
    }
   int choice=1;
    while(choice!=5)
     {
      
      printf("\n **************************************  ");
     printf("\n      Electricity Bill Calculator");
      printf("\n **************************************  ");
     
      
      printf("\n  MAIN MENU  ");
      printf("\n  1. Add new customer  "); 	
      printf("\n  2. Bill Generation  ");
      printf("\n  3. Display bill  ");
      printf("\n  4. Delete bill  ");
      printf("\n  5. EXIT ");
      printf("\n Enter your choice    ");
      scanf("%d",&choice);
       switch(choice)
        {
          case 1: add_customer();
                  break;
          case 2: generate_bill();
                  break;        
          case 3: display_bill();
                  break;
          case 4: delete_bill();
                  break;
          case 5: break;	  	  
          default:{
          	   printf("\n\n  Invalid Choice...!");
          	   printf("\n\n Press any key to continue..");
          	   getch();
      } 	          	  	
        }
     } 	
 }
  

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries such as stdlib.h, stdio.h, conio.h, string.h.
  • Now we will define a structure called “customer” using the “struct” keyword. It stores the customer’s name, ID, and price per unit of electricity.
  • Defining another structure called “bill”. It stores the bill’s ID, customer ID, name, price per unit and units consumed.
  • Now we will define the functions to give logic to our project.
  • The customer_id() function with return type int, will return the next available customer ID by reading the last customer ID stored in the “cus.txt” file.
  • The bill_id() function with return type int will return the next available bill ID by reading the last bill ID stored in the “bi.txt” file.
  • The add_customer() function will add a new customer by asking the user to enter the customer details and storing the data in the “cust.txt” file.
  • The generate_bill() function will generate a bill for a customer by asking the user to enter the customer ID and the number of units consumed. The function retrieves the customer details from the “cust.txt” file and calculates the total amount before storing the bill details in the “bill1.txt” file.
  • If entered ID doesn’t exist then using the if statement, the console will display an error statement -“Error! No such customer with id no.  %d exist.”.
  • The display_bill() function will display a customer’s bill by asking the user to enter the customer ID and bill ID. The function will retrieve the bill details from the “bill1.txt” file and displays the data on the console to the user.
  • The delete_bill() function will delete a bill by asking the user to enter the bill ID. The function reads the “bill1.txt” file and copies all the bills except the one with the specified ID to a new file named “temp.txt”. The function then deletes the original “bill1.txt” file and renames the “temp.txt” file to “bill1.txt”.
  • The main() function will contain interface where the system askes the user to enter the username and password. We will write a nested if-else control statement to display login validity. Then we will write the options this system will offer the admin to perform. A switch statement to execute the selected option. If the user enters an invalid option, the program will display an error message.
  • This sums up our project of Electricity Bill Generator using C++.

Advanced Atm Management System using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

  • Username : admin and Password: 12345

Electricity Bill generator using C++

  • Login is successful. The main menu will be displayed.

Electricity Bill generator using C++

  • Entering choice 1. The customer id has been generated.

Electricity Bill generator using C++

  • Record has been added successfully. No entering choice 2.

Electricity Bill generator using C++

  • Entering choice 3. Bill will be displayed with the total amount.

Electricity Bill generator using C++

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

Bank Management System using C++ (With Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodewithRandom



Leave a Reply