c++ 当我将字符串转换为 int 时,int 只是一个随机数?

标签 c++ string int

所以我有一个编辑值的函数,但我计算了 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.

来源:Documentation of stoi

关于c++ 当我将字符串转换为 int 时,int 只是一个随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934409/

相关文章:

javascript - 用于科学可视化的 C++/Javascript 通信方法

c++ - Boost.Log 问题

使用字符串的 Java 方法

java - 在java中连接2个二维数组?

java - 如何坚持用户输入是一个整数?

c++ - 当我定义 UNICODE 和/或 _UNICODE 时,非 unicode API 函数会发生什么情况?

c++ - Mac 上的 SDL2 + Opengl 黑窗

c# - 快速文本阅读(替代 File.ReadAllText() 和/或 StreamReader.ReadToEnd())

c# - 在 String.Format 中转义大括号 '{'

java - 在 Java 中连接两个整数以获得 float