2017-04-04 44 views
0

我一直在試圖找出這個名字生成器很長一段時間。我覺得我真的很接近,但有兩件事我無法弄清楚。這是一個星球大戰名稱生成器。在C++中使用SW名稱生成器的困難

我需要它,以便在同一行(我假設生成的名稱打印意味着我需要結合新做的名字和第二個名字。

,我有資本既第一困難,姓氏的第一個字母。我沒有爲它的第一個名字,不能提前搞清楚的第二個名字。

請幫幫忙。我附上下面的代碼。

感謝。

#include <string> 
#include <stdio.h> 
#include <iostream> 
#include <cctype> 
using namespace std; 

int main() 
{ 
    string name; 
    cout << "Please enter your name: " << endl; 
    getline(cin, name); 

    string birthTown; 
    cout << "Please enter the town you were born in: "; 
    cin >> birthTown; 

    string momMaiden; 
    cout << "Please enter your mother's maiden name: "; 
    cin >> momMaiden; 

    string firstPart = name.substr(0, 2); 
    for (int i = 0; i < name.length(); i++) 
    { 
     if (name[i] == ' ' && name[i + 1] != '\0') 
     { 
      name.substr(0,3); 
      cout << "Your Star Wars first name: "; 
      if (!firstPart.empty()) 
      { 
       int i; 
       for (i = 0; i < firstPart.length(); ++i) 
        firstPart[i] = tolower(firstPart[i]); 
      } 
      cout << name[i + 1]; 
      cout << name[i + 2]; 
      cout << name[i + 3]; 
      cout << firstPart << endl; 
     } 
    } 

    string secondName; 
    secondName = momMaiden.substr(0, 2) + birthTown.substr(0, 3); 
    if (!secondName.empty()) 
    { 
     int i; 
     for (i = 0; i < secondName.length(); ++i) 
      secondName[i] = tolower(secondName[i]); 
     cout << "Your Star Wars last name is: " << secondName << endl; 
    } 

    string fullName; 
    fullName = firstPart + " " + secondName; 
    int i; 
    if (fullName[i] == ' ')  //checks if an element is equal to a space 
    { 
     fullName[i] = toupper(fullName[i]); //space is set to upper case 
     fullName[i + 1] = toupper(fullName[i + 1]); //the element next to space is set to upper case 
     i++; //i is incremented 
     cout << "Your Star Wars last name is: " << fullName << endl; 

     return 0; 
    } 
} 

回答

0

看來你還沒有初始化變量並直接使用它:

int i; 
if (fullName[i] == ' ')  //checks if an element is equal to a space