this program is all about computing the bill of the electricity/.// the first 150kilowatts will be 10.00php... then additional kilowats will be add for another 20.00 per kilowatts...
In their bill also, there will be additional 5% for the tax...

And My prof give this condition in doing the program:
1.the first function should compute the amount w/o tax.
2. the secong will function computing the first function and with the tax.

thank you in advance whosoever heLp me in this...

problem: this source code is working.. but the thing is it does not give the accurate answer.

ASSIGNME.CPP

Dani AI

Generated

Brief diagnosis and what caused the wrong results: the thread already exposed the two concrete bugs that made the bill come out wrong — the program passed an uninitialized total into the tax routine (undefined behavior), and the tax routine treated the tax as a literal addition of 0.05 instead of computing 5% of the base amount. Also verify the threshold constant you use (the problem text says 150 kW but some code used 15). cleaned up the program structure and made main return an int, and pointed out the scanf/type and tax-calculation issues.

How to structure the functions so they match the professor's rules and avoid mistakes: have one function compute the base amount (no tax) and return that value. Compute the extra usage as max(0, usage - threshold) so extra charges never go negative. Make the tax function compute a percentage (for example, return base 0.05) or have a second wrapper function that returns base + base taxRate — but do not call the tax routine until after the base amount is computed. Add the tax to the base and print the final total formatted as currency (two decimals).

Practical, safety and portability tips: choose double if fractional kWh is allowed; check scanf return values or use fgets+sscanf to avoid input glitches; when reading a single-char repeat choice use a format or method that skips leftover whitespace; avoid nonstandard conio.h functions (clrscr, getch) for portable code; always validate inputs (no negative kWh) and return 0 from main.

Quick checklist to fix the bug you saw: compute base first, never pass uninitialized variables, compute tax as base * 0.05, clamp extra usage to zero if under the threshold, format money to two decimals, and validate user input. These steps address the undefined-behavior and percentage-miscalculation problems discussed above.

Recommended Answers

All 7 Replies

Why didn't you just post the code?..

#include<stdio.h>
#include<conio.h>
#include<math.h>


float compute_amount(int kilowatt);
float compute_tax(float total);

void main()
{clrscr();
int kilowatt;
float Amount;
char decide;
do{
printf("Please enter your kilowatt: ");
scanf("%d",&kilowatt);
Amount=compute_amount(kilowatt);
printf("Your current Bill for this month is: %f\n\n",Amount);
printf("Do you want to repeat your transaction[Y or N]:  ");
scanf("\n");
scanf("%c",&decide);
}
while(decide=='y'||decide=='Y');
getch();
}

float compute_amount(int kilowatt)
{float total;

	total=(kilowatt-15)*20+(10)+compute_tax(total);
	return total;
}


float compute_tax(float total)
{float totalwtax,tax=0.05;

totalwtax=total+tax;

return totalwtax;
}

Now to fix it up a little.. Ive indented the code, added spaces where appropriate, and made main return an int.

#include <stdio.h>
#include <conio.h>
#include <math.h>

float compute_amount(int kilowatt);
float compute_tax(float total);

int main() {
   clrscr();
   int kilowatt;
   float Amount;
   char decide;
   do {
      printf("Please enter your kilowatt: ");
      scanf("%d",&kilowatt);
      Amount = compute_amount(kilowatt);
      printf("Your current Bill for this month is: %f\n\n",Amount);
      printf("Do you want to repeat your transaction[Y or N]:  ");
      scanf("\n");
      scanf("%c", &decide);
   } while (decide=='y'||decide=='Y');
   getch();
   return 0;
}

float compute_amount(int kilowatt) {
   float total;
   total = (kilowatt - 15) * 20 + 10 + compute_tax(total);
   return total;
}


float compute_tax(float total) {
   float totalwtax, tax = 0.05;
   totalwtax = total + tax;
   return totalwtax;
}

One problem. scanf("%d",&kilowatt); kilowatt isn't an integer. Change the format specifier to what you'd use for a float.

Is this right? (kilowatt-15)*20+(10)+compute_tax(total); Haven't looked at it, just seems odd that you're taking 15 from kW.

Is this right? totalwtax=total+tax; Should it be * not +?

yahoOOOOOoooooooooOoooooooo.... Its working... i now it gives A accurate Answer!!! thank you very much!!thank you for your help!!! now, I can send this to my Prof.. GodbLEss ^_^

its working... but it does not including the tax??

its working but the output or the results does not including the Tax... hhmmm?? ^_T

I'd imagine you should change:

total = (kilowatt - 15) * 20 + 10 + compute_tax(total);

to

total = (kilowatt - 15) * 20 + 10;
total += compute_tax(total);

, but again check your formulae are correct.

hhm..
yeah.. now, it gives me accurate answer!!! thank you very much!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.