1

the assignment is the basic "cin a full name" and then "retrieve First Middle Last" bit, where you create a program that asks the user to type in their full first name into a single string and the programs picks apart the name and outputs it organized seperately. this is what i wrote:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string name;
    int index;
    index = name.find(' ');
    cin >> name;
    cout << "First name: " << name.substr(0, index) << endl;
    name = name.substr(index+1, name.length()-1);
    index = name.find(' ');
    cout << "Middle Name: " << name.substr(1, index) << endl;
    name = name.substr(index+1, name.length()-1);
    cout << "Last name: " << name;
    return 0;
}

the code just wont seperate them right, and will not redefine 'name' correctly. It always just bounces back to the beginning of the string. any help for a newbie? here's an example output:

Teenage Wonder Land
First name: Teenage
Middle Name: eenag
Last name: Teena
Process returned 0 (0x0)   execution time : 7.942 s
Press any key to continue.
4

5 回答 5

3

在输入控制台之前你不会找到任何东西,sbustr应该从索引中读取0

string name;
int index;
//index = name.find(' '); // comment out, name is empty, you won't find anything

cin >> name;
index = name.find(' '); // now you can find first space

cout << "Middle Name: " << name.substr(0, index) << endl;
//                                     ^

或者只是使用std::stringstream

  #include <sstream>

  std::stringstream ss(name);
  std::string token;
  int i = 0;
  while(ss >> token)
  {
    switch(i)
    {
      case 0: 
        std::cout << "First name: " << token << std::endl;
        break;
      case 1: 
        std::cout << "Middle name: " << token << std::endl; 
        break;
      case 2: 
        std::cout << "Last name: " << token << std::endl; 
        break;
      default:
        break;
      i++;
    }
  }
于 2013-09-14T03:21:32.250 回答
2

您显然无法在name为其分配值之前搜索某些内容,这就是您现在正在做的事情:

string name;
int index;

index = name.find(' ');  // No value assigned to name yet - nothing to search
cin >> name;             // Now you're giving it a value (too late)

相反,分配然后尝试找到一个值:

string name;
int index;

cin >> name;            // Assign a value first
index = name.find(' '); // Now try to find something in it
于 2013-09-14T03:24:43.447 回答
0

istream的提取运算符>>将抓取缓冲区中的所有非空白字符,直到遇到空白字符。

所以你在这里输入:

Teenage Wonder Land

包含 3 个空格,包括您点击时末尾的不可见换行符enter。从这里您应该能够弄清楚以下内容的作用:

cin >> name;

提示:name不包含您刚刚输入的整行。

于 2013-09-14T03:41:51.347 回答
0

我认为您应该使用std::getline一次来获取整行文本。目前您只阅读第一个单词(>>运算符只会提取到下一个空格字符的文本)。

std::string name;
if (std::getline(cin, name))
{
    // extraction successful, "name" should contain entire line
}

然后,您可以使用此问题中的其他答案之一或继续您自己的方法。

于 2013-09-14T04:07:36.217 回答
0
#include <iostream>
#include <string>

using namespace std;

string getnext(const string &full, const string &delim, size_t &beg) {
    size_t prev = beg;
    beg = full.find(delim, beg);
    if (beg != string::npos)
        return full.substr(prev, beg-prev);
    return full.substr(prev, full.length()-prev);
}

int main()
{
    string name, temp, error = "NameError: Enter first, middle, last";
    size_t index = 0;
    getline(cin, name); //Get the full name

    temp = getnext(name, " ", index); //Get first name
    if (index == string::npos) {
        cout << error;
        return -1;
    }
    cout << "First name: " << temp << endl;

    temp = getnext(name, " ", ++index); //Get middle name
    if (index == string::npos) {
        cout << error;
        return -1;
    }
    cout << "Middle Name: " << temp << endl;

    temp = getnext(name, " ", ++index); //Get last name
    cout << "Last name: " << temp << endl;
    return 0;
}
于 2013-09-14T05:18:57.470 回答