Stuck, Not sure how to get this to run

Hey Im getting an error at line 30.

Severity Code Description Project File Line Suppression State
Error (active) E0135 namespace "std" has no member "getline" TDU attempt C:\Users\Owner\source\repos\TDU attempt\TDU attempt\TDU attempt.cpp 30

Not sure what I am not seeing, but any help would be wonderful. Thanks

the error is at std::getline(std::cin >> std::ws, TDU);
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
#include <iostream>
#include <iterator> 
#include <map> 
using namespace std;
int main()
{
    //declaring variables
    string TDU;
    double usage;
    double total_charge;
    int run = 1;
    //declare a map to store names of TDU and montly fixed costs in dollars
    map<string, double> monthlycharges;
    monthlycharges.insert(pair<string, double>("ONCOR", 3.42));
    monthlycharges.insert(pair<string, double>("CENTERPOINT ENERGY", 5.47));
    monthlycharges.insert(pair<string, double>("AEP TEXAS CENTRAL", 9.00));
    monthlycharges.insert(pair<string, double>("AEP TEXAS NORTH", 10.53));
    monthlycharges.insert(pair<string, double>("TEXAS - NEW MEXICO POWER", 7.85));

    //declare a map to store names of TDU and usage costs in cents
    map<string, double> kwhcharges;
    kwhcharges.insert(pair<string, double>("ONCOR", 3.8447));
    kwhcharges.insert(pair<string, double>("CENTERPOINT ENERGY", 4.03120));
    kwhcharges.insert(pair<string, double>("AEP TEXAS CENTRAL", 4.84460));
    kwhcharges.insert(pair<string, double>("AEP TEXAS NORTH", 4.01990));
    kwhcharges.insert(pair<string, double>("TEXAS - NEW MEXICO POWER", 4.83210));

    while (run == 1) {
        cout << "Enter the name of TDU: " << endl;
        std::getline(std::cin >> std::ws, TDU);
        cout << "Enter the usage in kWh used: " << endl;
        cin >> usage;
        //declare iterator of maps
        map<string, double>::iterator it_monthlycharges;
        map<string, double>::iterator it_kwhcharges;
        //use the find function of map to get the pointer to correct entry according to key 'TDU'
        it_monthlycharges = monthlycharges.find(TDU);
        it_kwhcharges = kwhcharges.find(TDU);
        //calculate the charges
        total_charge = it_monthlycharges->second + (it_kwhcharges->second * 0.01 * usage);
        //print the final output
        cout << "TDU Delivery charges for " << TDU << " : " << total_charge << endl;
        cout << "Enter 1 to calculate bill for another month, 0 to exit" << endl;
        cin >> run;
    }
    return 0;
}
Last edited on
#include <string>
And use code tags when posting code.
AYe, Ill rework it and include #include <string>.

Ill use tags next time around, just found this site today.

Thank you for the help.
@TangentJay,
I tested your program with #include <string> in it and it worked fine, as far as I could tell.

Output: (Don't pay any attention to the stuff I entered, it was just weird random junk off the top of my head).
/users/max/tmp/bash/clang:$ ./pr1.cc
Enter the name of TDU: 
Thing
Enter the usage in kWh used: 
30026
TDU Delivery charges for Thing : 0
Enter 1 to calculate bill for another month, 0 to exit
1
Enter the name of TDU: 
Other Thing
Enter the usage in kWh used: 
1000000000
TDU Delivery charges for Other Thing : 0
Enter 1 to calculate bill for another month, 0 to exit
0
/users/max/tmp/bash/clang: Program exited with code#0

Looks good!

Using code tags:
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Best,
max
Hey Max, Thanks for the input, It will run up to

Enter the usage in kWh used and once you enter a number it gives me a Microsoft Visual C++ Runtime Library error.

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: C:\Users\Owner\source\repos\TDU attamp_t\Debug\TDU attamp_t.exe
File: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xtree
Line: 182

Expression: cannot dereference end map/set iterator

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---------------------------
Abort Retry Ignore
---------------------------

------------------------------------

I just ran this code in http://cpp.sh/ and it worked, I think I am missing something from Visual studios, maybe a addon I need to download? It will not run on my PC, but will run on the web compiler. Just tested it on two other compilers and it will run. It has to be my VS haha. THanks for the support.
Last edited on
Hm, did you try running it from the command line? I'm not too familiar with Visual Studio, but you can try running it form the command line and set this flag:
-std=c++2a
. I'm not sure if that would help, but you can try it.

Note: You should make your while loop a do-while instead, and initialize "run" to 0. That way you won't get infinite running if there's a mistake in it.
You should hardly ever need to explicitly declare a grotesque like map<string, double>::iterator. Just use auto. In general, don't delcare your variables before they're used. Declare them where they're needed and initialize them where you declare them.

1
2
        auto it_monthlycharges = monthlycharges.find(TDU);
        auto it_kwhcharges = kwhcharges.find(TDU);

Just use one map:

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
#include <iostream>
#include <map>
#include <string>
#include <limits>
using namespace std;

struct Charges {
	double month {};
	double kwh {};
};

double getNum(const std::string& prm)
{
	double i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}

int main()
{
	const map<string, Charges> charges {{"ONCOR", {3.42, 3.8447}},
		{"CENTERPOINT ENERGY", {5.47, 4.03110}},
		{"AEP TEXAS CENTRAL", {9.0, 4.84460}},
		{"AEP TEXAS NORTH", {10.53, 4.01990}},
		{"TEXAS - NEW MEXICO POWER", {7.85, 4.83210}}};

	double total_charge {};
	bool run {true};

	while (run && cin) {
		string TDU;

		cout << "Enter the name of TDU: ";
		std::getline(std::cin >> std::ws, TDU);

		if (const auto it_monthlycharges {charges.find(TDU)}; it_monthlycharges != charges.end()) {
			const auto usage {getNum("Enter the usage in kWh used: ")};

			total_charge = it_monthlycharges->second.month + (it_monthlycharges->second.kwh * 0.01 * usage);
			cout << "TDU Delivery charges for " << TDU << " : " << total_charge << '\n';
		} else
			cout << "TDU name not found\n";

		cout << "Enter 1 to calculate bill for another month, 0 to exit: ";
		cin >> run;
	}
}


Registered users can post here. Sign in or register to post.