问题描述
这是我的项目中只接受整数的代码:
int getInt(
const char* prompt = nullptr // User entry prompt
);
定义:
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;
}
我的提示有一个默认值“nullptr”,但 Valgrind 仍然显示此错误,它与 --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==
更新:初始化 char c
解决了问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)