2016-12-26 62 views
0

我是新来的C++,但确实有编码的基本知识。这个程序运行良好,但我想知道是否有更好的方法来做到这一点。有没有更好的方法?新来的c + +

该程序通过取您姓氏的前三个字母和您的名字的前两个字母来创建您的星球大战名字的第一个名称,从而形成一个星球大战的名称。然后你星球大战姓它需要你母亲的娘家姓的前两个字母,你出生在城市的前三个字母。

// 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; 
} 
+5

张贴在http://codereview.stackexchange.com/。 –

+0

感谢您的反馈,没有使用堆栈交换足以知道该怎么做。 – deadstone1991

+0

那么,你现在做... –

回答

0

您可以使用字符串:: SUBSTR这里存储字符而不是一次又一次地写出姓氏[0] ..姓氏[2]。

这里是字符串的示例:: 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; 
} 

输出:

think live in details.