如何获得第二个或第三个 'part' 的用户输入? C++

How to get second or third 'part' of user input? C++

用户输入为:

0 1 4 5

如何获取0并保存为整数,然后如何获取4并保存为整数?

情况二:

用户输入为:

0B11B3B76B

我怎样才能将所有这些(单独)保存到数组(字符串类型)中?

我知道这对你们中的一些人来说很简单,但那是我第一天使用 C++,我必须完成它。 .NET 永远!

您将不得不遍历输入字符串的每个字节,提取每个数字并将其作为整数转换为您的数组,如果您知道输入字符串的大小或字符串是固定的或具有最大长度。

类似于:

char myStr[12] = "0123456789";

int myArray[12];
int intCh;

for (intCh = 0; intCh < 12; intCh++) {
   /* Look for the [=10=] byte that terminates the string. */
   if (myStr[intCh] == '[=10=]')
       break;

   /* We need to cast the char to an int as we store it in the
    * array something like this.
    */
   myArray[intCh] = (int) myStr[intCh];
}

希望对您有所帮助,我已经有一段时间没有用 C 或 C++ 编写代码了,但它应该能为您提供一些指导...嘿,明白了吗?指点!

祝你好运。

补充一下,你的问题让我有点困惑,因为你一开始说你想将 int 转换为 char,然后你又说你想做相反的事情并将整数类型转换为字符串。

要做到这一点,您将使用相反的方法,您将获取整数数组的成员并将它们转换为 (char) 赋值:

myStr[intCh] = (char) myArray[intCh];

或者类似的东西 ;)