Hello Nik Hazreen,
Your first code is partly a good start, but you are repeating to much of the same code. And you are limited to only 3 houses. Not good.
You are either not understanding the instructions that you posted or there is more to what is needed. Also you are not following what you have posted.
It is best to post the full instructions that you were given and not what you think they mean.
Trying to follow the instructions I came up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
#include <iostream>
#include <iomanip>
#include <limits> // <--- Added.
#include <string> // <--- Added.
using namespace std; // <--- Best not to use.
double calculation(double);
constexpr double RATE{ 0.05 };
constexpr int WIDTH{ 10 };
constexpr unsigned MAXSIZE{ 10 };
int main()
{
const std::string PROMPT{ "\n Enter number of houses (-999 to quit): " };
constexpr int END{ -999 };
int count{}; // <--- ALWAYS initialize all your variables.
int numOfHouses{};
// <--- Kept for testing. Not really needed.
// Can be reduced to 2 variables: "unitCost" and "totalUnitCost".
double unit1{ 998.0 }, unit2{ 998.0 }, unit3{ 998.0 * 9 };
double totalunit1{ unit1 * RATE }, totalunit2{ unit2 * RATE }, totalunit3{ unit3 * RATE };
std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once.
while (std::cout << PROMPT && !(std::cin >> numOfHouses) || numOfHouses < 1 || numOfHouses > MAXSIZE)
{
if (!std::cin)
{
std::cout << "\n Invalid entry! Must be a number.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (numOfHouses == END)
{
break;
}
else if (numOfHouses < 1 || numOfHouses > MAXSIZE)
{
std::cout <<
"\n"
" Invalid number of houses! Try again.\n"
" Must be at least 1 to a maximum of " << MAXSIZE << ".\n";
}
}
do
{
if (numOfHouses == END)
{
continue; // <--- Skips anything that follows and goes to the while condition.
}
std::cout << '\n';
// TODO. Rest of code goes here.
count++; // <--- Moved.
} while (count < numOfHouses && numOfHouses != END);
if (numOfHouses != END)
{
cout <<
" Number of house(s): " << numOfHouses << "\n"
" House 1 : " << std::setw(WIDTH) << unit1 << "\n"
" House 2 : " << std::setw(WIDTH) << unit2 << "\n"
" House 3 : " << std::setw(WIDTH) << unit3 << "\n"
<< std::string(17, ' ') << "========\n"
" Total Units : " << std::setw(WIDTH) << unit1 + unit2 + unit3 << "\n"
" Total Payment : RM" << totalunit1 + totalunit2 + totalunit3 << "\n";
}
return 0;
}
|
The first while loop will end when a number 1 -10 is entered. It also checks for a non numeric number being entered because a non numeric entry like a letter will cause "cin" to be put in a failed state and unusable the rest of the program until corrected or the program is ended.
The do/while loop will allow you to enter up to 10 houses. I based this on your 2nd code having an array of 10.
At this point it would be very easy to add the class and create an array of classes in "main". Not sure if a class is the best choice here. I would have used a struct for what little there is. Either one will work. It depends on what you have learned so far and what you need practice on.
For your class consider this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Electric
{
double houseUsage{};
double totalUnitCost{};
public:
void accept()
{
cout << "\n Penggunaan rumah: "; // <--- Space before the (:) is not necessary. The space after is. Or your choice.
cin >> houseUsage;
totalUnitCost = calculation(houseUsage);
}
};
|
This stores the usage for each house or each element of the array along with the total cost for that house.
Back in "main" under the TODO part you can use a for loop to enter the usage for each house and add to 2 variables for total usage and total cost.
The next for loop would be used for print out each house in the array along with the usage and cost.
When that for loop is finished you would print the totals.
I have not started on the TODO part yet not knowing what you want to do.
The "cout" at the end of the program is more for demonstration than actual use. It will need to be adjusted a bit.
See what you think.
Andy