2016-12-26 61 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. 
相關問題