Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a second code I'm trying to run, My first code bricked, so I redid it and I'm still getting errors at temp 2 on line 48.

C++
// 2/26/2021
#include<iostream>

#include<string>
using namespace std;

int main()
{//these are values stored to be use later in the program.
int m, i, kwh;
string tde;
float sum1, sum2, temp1, temp2;

cout << "Enter number of months: " << endl;
cin >> m; //reading number of months
cout << "Enter your tde: " << endl;
cin.ignore();
getline(cin, tde); //reading the selected tde
i = 1;
sum1 = 0;
sum2 = 0;
for (i = 1;i <= m;i++)
{
cout << "Enter " << i << " month usage(kwh): ";
cin >> kwh;
temp1 = 9.95;
if (kwh >= 0 && kwh <= 1200)
temp1 = temp1 + 0.073; //logic to calculating EFL charges based on kwh
else if (kwh > 1200 && kwh <= 2000)
temp1 = temp1 + 0.037;
else if (kwh > 2000)
temp1 = temp1 + 0.077;

if (tde == "ONCOR")
temp2 = 3.42 + (0.0384470 * kwh);// kwh is value all ready stored
else if (tde == "CENTER POINT ENERGY")
temp2 = 5.47 + (0.0403120 * kwh);
else if (tde == "AEP TEXAS CENTRAL")
temp2 = 9.00 + (0.0448460 * kwh);
else if (tde == "AEP TEXAS NORTH")
temp2 = 10.53 + (0.0401990 * kwh);
else if (tde == "TEXAS-NEW MEXICO POWER")
temp2 = 7.85 + (0.0483210 * kwh);

sum1 += temp1;
sum2 += temp2; // getting the error at this line

cout << "Charges for " << i << " month based on charges specified in EFL: " << temp1 << endl;
cout << "Charges for " << i << " month in " << tde << ": " << temp2 << endl;
cout << " Total charge for " << i << " month is: " << (temp1 + temp2) << endl << endl; //each month total bill
}
cout << endl;
cout << "Charges for " << m << " months based on charges specified in EFL: " << sum1 << endl;
cout << "Charges for " << m << " months in " << tde << ": " << sum2 << endl;
cout << " Total charge for " << m << " months is: " << (sum1 + sum2) << endl;
}
The code will run up to this point and then it stops. Second time posting on this subject, apologizes, first time coding something this big.

Severity Code Description Project File Line Suppression State
Warning C6001 Using uninitialized memory 'temp2'. TDU attempt C:\Users\Owner\source\repos\TDU attempt\TDU attempt\TDU attempt.cpp 48

Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "name" is undefined TDU attempt C:\Users\Owner\source\repos\TDU attempt\TDU attempt\TDU attempt.cpp 7

What I have tried:

I'm not sure, I remade it and I'm still getting more issues, even when looking around the net.
Posted
Updated 2-Mar-21 21:41pm
v2

1 solution

Look at your code:
C++
if (tde == "ONCOR")
temp2 = 3.42 + (0.0384470 * kwh);// kwh is value all ready stored
else if (tde == "CENTER POINT ENERGY")
temp2 = 5.47 + (0.0403120 * kwh);
else if (tde == "AEP TEXAS CENTRAL")
temp2 = 9.00 + (0.0448460 * kwh);
else if (tde == "AEP TEXAS NORTH")
temp2 = 10.53 + (0.0401990 * kwh);
else if (tde == "TEXAS-NEW MEXICO POWER")
temp2 = 7.85 + (0.0483210 * kwh);

sum1 += temp1;
sum2 += temp2; // getting the error at this line
What if tde is not one of these values: "ONCOR", "CENTER POINT ENERGY", "AEP TEXAS CENTRAL", "AEP TEXAS NORTH", or "TEXAS-NEW MEXICO POWER"?
What value does temp2 contain then?

That's what the system is telling you: there is a path through your code where you start using a variable that has not been given any value.

Probably, you need an else clause to your if ... else if ... set to detect when the user types something you didn't expect, and handle it gracefully.

Oh, and do yourself a big favour: indent your code. I don't care what indentation scheme you use as long as you are consistent about it, but if you don't indent it it becomes harder to read and more prone to errors.
 
Share this answer
 
v2
Comments
CPallini 3-Mar-21 4:22am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900