c++ - 输入操作

标签 c++

考虑这个问题
用户将按
的顺序输入一维数组(char test[100]) 字母/空格/索引/空格/字母....
像这样
a 1 s 1 e 3 r 4 r 3 t 3 直到 'enter' 被击中。
索引和字母一样可以重复。
所以上面的序列应该意味着 test[1]=a ,test[1]=s 等等。
为了构建这个问题,我认为我应该测试输入的字符是否为换行符。
但我不知道该怎么做

你能推荐一些代码吗

最佳答案

放弃整个想法;你的问题是错误的。用户没有输入char[100]。相反,用户输入一个字符串。突然之间变得非常简单:

#include <string>
#include <iostream>

int main()
{
    std::string user_input;

    std::getline(std::cin, user_input);

    // done: now use user_input
}

现在您可以遍历字符串、对其进行标记化或其他任何操作。例如:

std::istringstream iss(user_input);
char c;
int n;

while (iss >> c >> n)
{
    std::cout << "We have a pair (" << c << ", " << n << ")\n";
}

关于c++ - 输入操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9445140/

相关文章:

c++ - 是否允许在 JSON 文件中对 boost read_json 进行注释?

python - 在 Python 中使用 dlib 训练对象检测器

C++ 派生类无法正常运行

c++ - 用于编译多个 mex 函数的 Makefile

c++ - condition_variable::wait_for 和 wait_until 使用 chrono::steady_clock 但在 PC sleep 时跳过持续时间?

c++ - 为什么使用 GetModuleHandle 获取基地址有效?

c++ - std::ifstream 因某种原因关闭?

c++ - 从 boost::hash 获取 32 位哈希值

c++ - 有没有人尝试在 C++ 中实现类似 Haskell 的 case-of 语句?

c++ - 使用反转距离的 K 均值聚类