2016-12-26 2 views
0

私はC++を初めて使っていますが、コーディングの基礎知識はあります。このプログラムはうまく動作しますが、これを行うにはより良い方法があるのだろうかと思います。は良い方法ですか? new to C++

このプログラムでは、あなたの姓の最初の3文字とあなたのファーストネームの最初の2文字を使ってスターウォーズの名前を作ることでスターウォーズの名前をつけます。そして、あなたの星のための戦争は、それはあなたの母親の旧姓の最初の2つの文字と、あなたがに生まれた都市の最初の3つの文字を取り姓。

// 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. 
関連する問題