This is my code for accepting only integers in my project:
int getInt(
const char* prompt = nullptr // User entry prompt
);
Definition:
int getInt(const char* prompt)
{
bool error1, error2;
int intg;
char c;
if (prompt)cout << prompt;
do
{
error1 = false;
error2 = false;
cin >> intg;
error2 = cin.fail();
cin.get(c);
error1 = c != '\n';
if (error2)
{
cin.clear();
clearinput();
cout << "Bad integer value, try again: ";
}
else if (error1)
{
cin.clear();
clearinput();
cout << "Enter only an integer, try again: ";
}
} while (error1 || error2);
return intg;
}
I have a default value of "nullptr" for my prompt but still Valgrind showing this error, it is with --track-origins=yes
==161949== Conditional jump or move depends on uninitialised value(s)
==161949== at 0x402599: sdds::getInt(char const*) (utils.cpp:55)
==161949== by 0x40116F: getIntTester() (ms1Tester_prof.cpp:35)
==161949== by 0x401065: main (ms1Tester_prof.cpp:19)
==161949== Uninitialised value was created by a stack allocation
==161949== at 0x4024C4: sdds::getInt(char const*) (utils.cpp:49)
==161949==
Aucun commentaire:
Enregistrer un commentaire