c++ - 我如何将用户输入分成一个字符和一个整数?

标签 c++

我正在做一个项目,我必须从用户输入中解析数据。

#include <iostream>   // for cin and cout
#include <iomanip>    // for setw()
#include <cctype>     // for toupper()
using namespace std;


int main(){

string playerInput;

cin >> playerInput; 
//Player would input strings like C13,C 6, I1, Z  16, etc...




}
return 0;
我试过这样的事情,这有点工作,但只有当字母处理字符串中的数字时。
int myNr = std::stoi(playerInput);
我的最终目标是从字符串中获取字母和数字,并将它们分别放入一个 char 变量和一个整数变量中。我被困在如何从这里开始,可以使用一些帮助,谢谢!

最佳答案

这是实现这一目标的最简单和最短的方法(它还忽略空格和制表符):

int main() {
    char ch;
    int n;
    cin >> ch >> n;

    cout << "ch = " << ch << ", n = " << n << endl;
}
我认为其他答案有点过于复杂。

关于c++ - 我如何将用户输入分成一个字符和一个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62747934/

相关文章:

c++ - 其他类的初始化类 - C++

c++ - VS2013 错误 : LNK2019 When trying to build ZeroMQ server

c++ - 为什么我不能在函数中使用 constexpr 值,但我可以在这个值的范围内做同样的事情?

c++ - 有没有办法将游戏列表信息从 Steam 获取到桌面应用程序中?

c++ - DirectX 11 将多个纹理加载到Texture2DArray中

c++ - 退出(0)与返回 0

c++ - std::accumulate C++20 版本

c++ - 我的 QList 类列表在这里存储什么

c++ - g++ 关于声明多维双数组的警告

c++ - txt 中的 vector 垫