嗨,我想寫一個C++程序,用戶將輸入一個名稱可以說,例如:塔米阿拉姆汗Rifat和計算機將打印名稱的格式化版本,在這種情況下,將是:T. A. K. Rifat先生。我已經包含下面的代碼。你將能夠看到我接近但仍然不是我想要的。請幫忙。除了姓以外,我如何取名字的所有部分的首字母?
#include<iostream>
#include<string>
using namespace std;
class myclass{
private:
string name,temp;
string p;
int i,j,sp;
public:
void work(){
cout << "Enter the name of the male student: ";
getline(cin,name);
cout << endl;
cout << "The original name is: ";
cout << name;
cout << endl << endl;
cout << "The formatted name is: " << "Mr." << name[0] << ".";
for(i=0;i<name.size();i++){
if(name[i]==' '){
sp=i;
for(j=sp+1;j<=sp+1;j++){
temp=name[j];
cout << temp << ".";
}
}
}
for(i=sp+2;i<name.size();i++){
cout << name[i];
}
cout << endl;
}
};
int main(){
myclass c;
c.work();
}
我們可以看到一些輸出嗎? – Resource 2014-12-05 14:00:37
請修復您的格式。只需簡單地縮進所有代碼,而不是將反引號放在任何地方。 – leemes 2014-12-05 14:02:24