有更好的办法吗?c+的新功能+; 我是C++新手,但对编码有一定的了解。这个程序运行良好,但我想知道是否有更好的方法来实现这一点

有更好的办法吗?c+的新功能+; 我是C++新手,但对编码有一定的了解。这个程序运行良好,但我想知道是否有更好的方法来实现这一点,c++,C++,该程序通过取你姓氏的前三个字母和名字的前两个字母来生成你的星球大战名字。然后,你的姓氏以你母亲婚前姓的前两个字母和你出生城市的前三个字母为准 // starWarsName.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; int ma

该程序通过取你姓氏的前三个字母和名字的前两个字母来生成你的星球大战名字。然后,你的姓氏以你母亲婚前姓的前两个字母和你出生城市的前三个字母为准

// starWarsName.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string> 
using namespace std;


int main()
{
    string firstName; 
    string surname; 
    string maidenName;
    string city;
    cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;

    cout << "please enter your first name" << endl;
    cin >> firstName;
    cout << "please enter your surname" <<endl;
    cin >> surname; 
    cout << "what is your mothers maiden name?" << endl;
    cin >> maidenName;
    cout << "please tel me which city you were born in" << endl;
    cin >> city; 

    cout << firstName << " " << surname << endl;
    cout << firstName[0] << " " << surname << endl;

    int size = firstName.length();
    //cout << size;
    cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
    cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];

    cin.get();
    cin.ignore();

    return 0;
}
//starWarsName.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int main()
{
字符串名;
串姓;
弦首名;
字符串城市;

cout您可以在这里使用string::substr来存储字符序列,而不必一遍遍地写姓氏[0]…姓氏[2]

下面是string::substr

#include <iostream>
#include <string>

int main ()
{
std::string str="We think in generalities, but we live in details.";
                                       // (quoting Alfred N. Whitehead)

std::string str2 = str.substr (3,5);     // "think"

std::size_t pos = str.find("live");      // position of "live" in str

std::string str3 = str.substr (pos);     // get from "live" to the end

std::cout << str2 << ' ' << str3 << '\n';

return 0;
}

谢谢你的反馈,不用堆栈交换,知道怎么做。现在,你马上就做了……(不仅仅是C++,而且还使用其他编程语言):在输入输入之后,一定要检查输入是否成功。
think live in details.