C++ 在一行中输入更多字符串

C++ 在一行中输入更多字符串,c++,C++,我看到一条线,看起来像: 名称1-名称2示例:Josh-Marc 我需要将这两个名称作为两个不同的字符串。 我不知道怎样才能避免这一点——在读那一行的时候。。 有什么想法吗? 提前感谢,如果已知字符串模式,则可以解析它 例: 这是演示代码。你也可以这样做 #include <stdio.h> #include <iostream> using namespace std; int main() { char string[20], name1[20], name2

我看到一条线,看起来像: 名称1-名称2示例:Josh-Marc 我需要将这两个名称作为两个不同的字符串。 我不知道怎样才能避免这一点——在读那一行的时候。。 有什么想法吗?
提前感谢,

如果已知字符串模式,则可以解析它 例:

这是演示代码。你也可以这样做

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   char string[20], name1[20], name2[20];
   gets(string);
   int j = 0, k = 0;
   int i = 0;
   while(string[i] != '-')
   {
     name1[i] = string[i];
     i++;
   }
   while(string[i] != '\0')
     name2[j++] = string[++i];

   cout<<name1<<" "<<name2;
 }

称我为老式,但我尝试编写一些代码来实现这一点。可能的重复只是将字符串拆分为标记,然后去掉任何“-”标记。。。这是一个简单的解决方案。是的,我知道这一点,但如何将其作为输入来阅读呢?如果输入是Josh-Marc 1行?我不能只使用cin,因为有空格。如果这是您的问题,也许您应该在问题中提到,您不知道如何以您想要的方式获取输入。@Artegor您可以使用以下std::getline std::cin,name;读取行。我建议你在使用之前
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
   char string[20], name1[20], name2[20];
   gets(string);
   int j = 0, k = 0;
   int i = 0;
   while(string[i] != '-')
   {
     name1[i] = string[i];
     i++;
   }
   while(string[i] != '\0')
     name2[j++] = string[++i];

   cout<<name1<<" "<<name2;
 }