c++ - Unintialized local variable 'e' used -
i have code. should work perfectly. it's circle calculator; i'm doing exercise. want user have option return the 'main menu.' made yes/no prompt using char* e; it's uninitialized. how can initialize
#include <iostream> using namespace std; class circlecalc { public: double const pi = 3.1415962543; double diameter; double radius; double circumference; }; int _welcome() { circlecalc calc; cout << endl; int = 0; char* e; cin >> i; while (i != 5) { switch (i) { case(1) : cout << "enter radius." << endl; cin >> calc.radius; cout << endl; cout << (calc.radius * 2) * calc.pi << endl; cout << "exit? [y/n]" << endl; cin >> e; if (e == "y") { _welcome(); } else if (e == "n") { } else { cerr << "unsupported function" << endl; } case(2) : cout << "enter diameter" << endl; cin >> calc.diameter; cout << endl; cout << (calc.diameter * 2) * calc.pi << endl; cout << "exit? [y/n]" << endl; cin >> e; if (e == "y") { _welcome(); } else if (e == "n") { } else { cerr << "unsupported function" << endl; } break; case(3) : cout << "enter circumference" << endl; cin >> calc.circumference; cout << endl; cout << (calc.circumference / 2) / calc.pi; cout << "exit? [y/n]" << endl; cin >> e; if (e == "y") { _welcome(); } else if (e == "n") { } else { cerr << "unsupported function" << endl; } break; case(4) : cout << "enter circumference" << endl; cin >> calc.circumference; cout << endl; cout << calc.circumference / calc.pi; cout << "exit? [y/n]" << endl; cin >> e; if (e == "y") { _welcome(); } else if (e == "n") { } else { cerr << "unsupported function" << endl; } break; case(5) : return(0); break; default: cerr << "unsupported function." << endl; } } }
instead of:
char* e;
use:
std::string e;
the reason get:
unintialized local variable 'e' used
is e
not set when passed operator>> used cin, initialize assign array it, ie:
char arr[128] = {0}; char* e = arr;
operator>>
cin stream expect have provided memory buffer read string located, char* e;
not bound such buffer, , end in (possibly) crash (undefined behaviour).
Comments
Post a Comment