简体   繁体   English

一门课程的初学者C ++程序

[英]Beginner's C++ program for a class

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. 分配是基本的“ cin全名”,然后是“检索名中”,您在其中创建一个程序,要求用户将其全名键入单个字符串,然后程序将名称分开并输出它是分开组织的。 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.

You wont' find anything before type your in console and sbustr should read from index 0 在控制台中键入内容之前,您将找不到任何内容,并且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;
//                                     ^

Or just use std::stringstream 或者只使用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++;
    }
  }

You clearly can't search for something in name before you assign it a value, which is what you're doing now: 您显然无法在为name分配值之前搜索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)

Instead, assign and then try to find a value: 相反,分配, 然后尝试找到一个值:

string name;
int index;

cin >> name;            // Assign a value first
index = name.find(' '); // Now try to find something in it

The extraction operator >> for istream will grab all non-whitespace characters in the buffer until it encounters a whispace. istream的提取运算符>>将捕获缓冲区中的所有非空白字符,直到遇到空白为止。

So your input here: 所以你在这里输入:

Teenage Wonder Land

contains 3 whitespaces including the invisible newline at the end when you hit enter . 包含3个空格,包括在按下Enter键时结尾的不可见换行符。 From this you should be able to figure out what the following does: 由此您应该能够弄清楚以下内容:

cin >> name;

Hint: name doesn't contain the entire line you just entered. 提示: name不包含您刚刚输入的整个行。

I think you should use std::getline to get the entire line of text at once. 我认为您应该使用std::getline一次获取整行文本。 Currently you are only reading in the first word ( >> operator will only extract text up to the next whitespace character). 当前,您只读第一个单词( >>运算符只会将文本提取到下一个空格字符)。

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

Then, you can use one of the other answers in this question or continue with your own approach. 然后,您可以在此问题中使用其他答案之一,或者继续使用自己的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM