所以我有一个编辑值的函数,但我计算了 main 中的值以查看它输出 1309668848 并在每次运行程序时更改。 (这不会在预处理器中发生)。我为此苦恼了一段时间,决定来这里寻求建议。
这是函数。
void GetDahInt() {
std::string NewValueS;
getline(std::cin, NewValueS);
NewValue = (int)NewValueS.c_str();
}
最佳答案
你可以使用
std::stoi( str )
Discards any whitespace characters (as identified by calling isspace()) until the first non-whitespace character is found, then takes as many characters as possible to form a valid base-n (where n=base) integer number representation and converts them to an integer value.
关于c++ 当我将字符串转换为 int 时,int 只是一个随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934409/