Hello TangentJay,
Sorry for the delay. I had a doctors appointment today that really screwed up the day.
Unless you feel that rewriting your program is worth it there is nothing I can see wrong with your program except adding the menu.
If you would ask 10 people to writ a menu you will most likely get 10 different versions. There is nothing with
agent max's version, but I took this approach:
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
|
std::string tdeNames[]
{
"",
"ONCOR\n",
"CENTER POINT ENERGY\n",
"AEP TEXAS CENTRAL\n",
"AEP TEXAS NORTH\n",
"TEXAS-NEW MEXICO POWER\n"
};
constexpr unsigned int MAXTDES{ sizeof(tdeNames) / sizeof(tdeNames[0]) };
std::cout << '\n';
do
{
for (int idx = 1; idx < MAXTDES; idx++)
{
std::cout << ' ' << idx << ". " << tdeNames[idx];
}
while (std::cout << " Enter your choice: " && !(std::cin >> tdeChoice) || tdeChoice < 1 || tdeChoice > MAXTDES - 1)
{
if (!std::cin)
{
std::cerr << "\n Invalid Entry! Must be a number.\n\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (tdeChoice < 1 || tdeChoice > MAXTDES - 1)
{
std::cerr << "\n Invalid choice! try again.\n\n";
}
}
} while (tdeChoice < 1 || tdeChoice > MAXTDES - 1);
switch (tdeChoice)
{
case 1:
tde = "ONCOR";
break;
case 2:
tde = "CENTER POINT ENERGY";
break;
| |
What I find advantageous to doing the "string" array this way is it makes it easy to add to. At the end of line 8 put a comma and press "Enter" the in a set of double quotes type "EXIT\n". Not real important right now, but will have a use later.
The "switch", if/else if if you are not use to the "switch", will set the variable "tde" to the proper name, so you do not have to relay on the user to get it right.
The other thing I noticed is at the top of "main" you define "i". Later before the for loop you set "i =1" then in the for loop you start with "for (int i = 1;" The "i" in the for loop overshadows the "i" defined at the beginning of "main" making it effectively useless. Since it is better to define the loop iterator in the for loop I took out the first "i" since it is never used.
I did not do anything with these variables "temp1" and "temp2", but they are not the greatest names, but if you define them inside the for loop at least they are local to the for loop which makes them slightly better, but not by much.
By the time you reach:
1 2 3 4 5
|
cout
<< '\n'
<< "Charges for " << m << " months based on charges specified in EFL: " << sum1 << '\n'
<< "Charges for " << m << " months in " << tde << ": " << sum2 << '\n'
<< " Total charge for " << m << " months is: " << (sum1 + sum2) << '\n';
| |
Try to avoid using single letters for variable like it is the "Black Death" or "COVID-19". The variables "m" has never changed. "tde" is set to the correct name and "sum1" and "sum2" have values from the for loop.
Once you get the program working the way that you want I would consider adding:
1 2 3
|
do
{
} while (tdeChoice != MAXTDES - 1);
| |
Put the "do" before line 12 in the above example and the close of the loop after the last "cout" statement. This will make use of the "Exit" added to the menu and allow you to continue the program to make another test.
The part I have not checked is if there may be any variable the may need to be cleared or zeroed out before another run. You may simply be able to move the lines:
1 2 3
|
int m{}, /*i{},*/ kwh{}/*, tdeChoice{}*/; // <--- "i" never used. "tdeChoice" needs to remain outside the do/while loop.
string tde;
double sum1, sum2{}, temp1{}, temp2{}; // <--- "temp1" and "temp2" should be defined in the for loop.
| |
inside the do/while loop. This way they will be destroyed when the loop ends and then created if the loop continues.
I think if you can put all that together you would have a nice program. You may need to work on the output to add some blank lines to make it easier to read.
Andy