Run-Time Check Failure #3 - The variable 'temp2' is being used without being initialized.

Pages: 12
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
Hey Andy, Thanks for the Input. I threw out this code and remade a different one from scratch with the help that was posted here. I see the basic? error That was there haha. Thanks all for your help and tips.
If you would ask 10 people to writ a menu you will most likely get 10 different versions


More! - as at least one person will provide at least 2 different versions! :) :)
@Handy Andy,
Yours is a bit more complicated but it does also have input validation. I've never used "constexpr", what data type is that?
@agent max,

"constexpr" is similar to "const", but a little more involved. This became available in the C++11 standards.

Have a look at https://en.cppreference.com/w/cpp/language/constexpr This should explain it better than I can.

Andy
That makes sense. Thanks!
Registered users can post here. Sign in or register to post.
Pages: 12