Mark
Mark

Reputation: 13

C Program Displays multiple prints

i have written an c code to calculate electric bill its giving the output i expect but it repeats the output too much i have tried to debug it but can't seem to know where did i mistake can someone review my code please ? i just want the first output to be visible only not all of those results

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

int main(){
    GetInputs();
    CalConsumCharge();
    CalExtraCharges();
    DispBill();
}

void GetInputs(){
    int unittot;
    int unit1;
    int Prev_Cons;
    printf("Enter Previous units consumption: ");
    scanf("%d",&Prev_Cons);
    printf("Enter the units consumed: ");
    scanf("%d",&unittot);
    unit1 = unittot - Prev_Cons;
    CalConsumCharge(unit1);
}

void CalConsumCharge(int unit){
    int total = 0;
    if(unit<=50){
        total = (unit*22);
    }
    else if(unit<=100){
        total = ((50*22)+(unit-50)*30);
    }
    else if(unit<=200){
        total = ((50*22)+(50*30)+(unit-100)*36);
    }
    else if(unit<=350){
        total = ((50*22)+(50*30)+(100*36)+(unit-200)*70);
    }
    else if(unit<=650){
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(unit-350)*90);
    }
    else if(unit<=1000){
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(300*90)+((unit-650)*135));
    }
    else{
        total = ((unit*145));
    }

    CalExtraCharges(total);
}

void CalExtraCharges(int total){
    int total1;
    total = total/100;
    if(total>=2000){
        total1 = total + 10 +20;
    }
    else{
        total1 = total +10;
    }
    DispBill(total1);
}

void DispBill(int total1){
    if(total1>=2000){
        printf("A penalty Charge was Placed Because your bill is over 2000 L.E");
        printf("Bill amount is: %d L.E",total1);
    }
    else if(total1<2000) {
        printf("your bill is : %d L.E",total1);
    }
}

Upvotes: 1

Views: 55

Answers (2)

Hitokiri
Hitokiri

Reputation: 3699

You should compile your code, then see what happen (errors, warning).

Firstly, you forgot to declare the functions. Add the declarations below before implementation of the main function.

void GetInputs();
void CalConsumCharge(int unit);
void CalExtraCharges(int total);
void DispBill(int total1);

Secondly, because you call the CalConsumCharge in GetInputs, CalExtraCharges in CalConsumCharge, etc, so you just call GetInputs in the main function is enough.

int main(){
    GetInputs();
}

If you want to call many functions as your code in the main function, you should return the value at the end of each function as below:

The declaration of all function:

int GetInputs();
int CalConsumCharge(int unit);
int CalExtraCharges(int total);
void DispBill(int total1);

The, correcting a bit for each function

int GetInputs() {
   ...
   unit1 = unittot - Prev_Cons;
   return unit1;
}
int CalConsumCharge(int unit) {
    ...
    else{
        total = ((unit*145));
    }
    return total;
}
int CalExtraCharges(int total) {
   ...
   else{
        total1 = total +10;
    }
    return total1;
}

Then, in main function:

int main () {
   int unit1 = GetInputs();
   int total = CalConsumCharge(unit1);
   int total1 = CalExtraCharges(total);
   DisBill(total1);

   return 0;
}

Upvotes: 1

PatrickOfThings
PatrickOfThings

Reputation: 239

Ok, this code needs a lot of love and attention. I am surprised you can compile and run it. You main function only needs to call GetInputs() as that calls all the other functions.

I refactored the main and the rest of the code for cleanliness. I didn't change any logic and it ran fine. Take a look:

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

void DispBill(int total1)
{

    if(total1>=2000)
    {
        printf("A penalty Charge was Placed Because your bill is over 2000 L.E");
        printf("Bill amount is: %d L.E",total1);
    }
    else if(total1<2000) 
    {
        printf("your bill is : %d L.E",total1);
    }
}

void CalExtraCharges(int total)
{
    int total1;
    total = total/100;

    if(total>=2000)
    {
        total1 = total + 10 +20;
    }
    else
    {
        total1 = total +10;
    }

    DispBill(total1);
}

void CalConsumCharge(int unit)
{
    int total = 0;

    if(unit<=50)
    {
        total = (unit*22);
    }
    else if(unit<=100)
    {
        total = ((50*22)+(unit-50)*30);
    }
    else if(unit<=200)
    {
        total = ((50*22)+(50*30)+(unit-100)*36);
    }
    else if(unit<=350)
    {
        total = ((50*22)+(50*30)+(100*36)+(unit-200)*70);
    }
    else if(unit<=650)
    {
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(unit-350)*90);
    }
    else if(unit<=1000)
    {
        total = ((50*22)+(50*30)+(100*36)+(150*70)+(300*90)+((unit-650)*135));
    }
    else
    {
    total = ((unit*145));
    }

    CalExtraCharges(total);
}

void GetInputs(void)
{
    int unitTotal;
    int unitOne;
    int Prev_Cons;

    printf("Enter Previous units consumption: ");
    scanf("%d",&Prev_Cons);

    printf("Enter the units consumed: ");
    scanf("%d",&unitTotal);

    unitOne = unitTotal - Prev_Cons;

    CalConsumCharge(unitOne);
}

int main()
{
    GetInputs();
}

Here is my output:

Here is the output!

Upvotes: 0

Related Questions