Your indentation continues to be lousy - you'll lose marks.
Code:
#include <stdio.h>
int main()
{
int times = 1, type;
double power, cost, TotalPower, TotalCost, max = 0, min = 0, current, voltage;
do {
printf("\nRecipient #%d information:\n", times);
printf("\nInsert current > ");
scanf("%lf", ¤t);
printf("\nInsert voltage > ");
scanf("%lf", &voltage);
if (times == 1 && current == -1 && voltage == -1) {
printf("no recipient information is inserted ");
return 0;
}
printf("\nEnter Consumption Category:\n");
printf("1 = Residential\n");
printf("2 = Commercial\n");
printf("3 = Agricultural & charities\n");
printf("4 = Government \n");
printf("5 = Industrial \n");
printf("6 = Private Educational and Medical\n");
scanf("%d", &type);
switch (type) {
case 1:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts\n", power);
if (power > 6000) {
cost = (6000 * 0.18) + ((power - 6000) * (0.3));
printf("The cost = %.2lf Rs\n", cost);
} else {
cost = 6000 * 1.8;
printf("The cost = %.2lf RS\n", cost);
}
break;
case 2:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts\n", power);
if (power > 6000) {
cost = (6000 * 0.20) + ((power - 6000) * (0.30));
printf("The cost = %.2lf RS\n", cost);
} else {
cost = 6000 * 0.2;
printf("The cost = %.2lf RS\n", cost);
}
break;
case 3:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts", power);
if (power > 6000) {
cost = (6000 * 0.16) + ((power - 6000) * (0.20));
printf("The cost = %.2lf RS\n", cost);
} else {
cost = 6000 * 0.16;
printf("The cost = %.2lf RS\n", cost);
}
break;
case 4:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts", power);
if (power > 6000) {
cost = (6000 * 0.32) + ((power - 6000) * (0.32));
printf("The cost = %.2lf RS\n", cost);
} else {
cost = 6000 * 0.32;
printf("The cost = %.2lf RS\n", cost);
}
break;
case 5:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts\n", power);
if (power > 6000) {
cost = (6000 * 0.18) + ((power - 6000) * (0.18));
printf("The cost = %.2lf RS\n", cost);
} else {
cost = 6000 * 0.18;
printf("The cost = %.2lf RS\n", cost);
}
break;
case 6:
power = current * voltage / 1000;
printf("Power =%.2lf kilo watts\n", power);
if (power > 6000) {
cost = (6000 * 0.18) + ((power - 6000) * (0.18));
printf("The cost = %.2lf RS\n", cost);
} else {
cost = 6000 * 0.18;
printf("The cost = %.2lf RS \n", cost);
}
break;
default:
printf("Incorrect Catrgory ! program will stop now ! ");
return 0;
}
if (current == -1 || voltage == -1)
TotalPower = TotalPower + power;
TotalCost = TotalCost + cost;
if (cost <= min)
min = cost;
if (cost >= max)
max = cost;
times += 1;
}
while (!(current == -1 && voltage == -1));
printf("\nthe total power is : %.2lf \n", TotalPower);
printf("\nThe total cost is : %.2lf RS \n", TotalCost);
printf("\nthe min is : %.2lf\n ", min);
printf("\nthe max is : %.2lf\n", max);
return 0;
}
Some of your variable names are meaningless, again - marks lost.
> printf("\nRecipient #%d information:\n", times);
Replace 'times' with say 'customerNumber'.
> THE problem is i have to use the switch here and the function make this more difficult
Where exactly does it say in the PDF that you have to use a switch statement?
Sooner or later, you're going to have to start dealing with functions. Putting everything in main() just isn't a sustainable approach.
Study this latest addition to your code.
Code:
if (current == -1 || voltage == -1)
TotalPower = TotalPower + power;
TotalCost = TotalCost + cost;
The scope of the if statement is only the next line.
Do you really mean to calculate TotalPower on the very last iteration?