初学者的C ++程序

时间:2013-09-14 03:14:26

标签: c++

作业是基本的" cin全名"然后"检索First Middle Last" bit,您在其中创建一个程序,要求用户将其完整的名字输入到单个字符串中,程序会分开名称并将其输出分开组织。这就是我写的:

#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;
}

代码只是不会将它们分开,并且不会重新定义&#39; name&#39;正确。 它总是弹回到字符串的开头。 对新手有什么帮助吗? 这是一个示例输出:

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.

5 个答案:

答案 0 :(得分:3)

你不会&#39;在键入您的控制台之前找到任何内容,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++;
    }
  }

答案 1 :(得分: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

答案 2 :(得分:0)

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

所以你在这里输入:

Teenage Wonder Land
当你点击 时,

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

cin >> name;

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

答案 3 :(得分:0)

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

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

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

答案 4 :(得分: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;
}