ユーザレジスタを作成するプロセスでは、double do {} whileループが表示され、と同じ条件になります。文字列内に空白が検出された場合、読み取りが利用できなくなります。2つのループ内の文字列のスペースを確認する
void createAccount()
{
unsigned short int i = 0;
bool space = false;
cin.ignore();
cout << "FIRST NAME: ";
getline(cin, fullName[0]);
do {
cout << "MIDDLE NAME: ";
getline(cin, fullName[1]);
for (i = 0; i < fullName[1].size(); i++)
{
if (fullName[i][1] == 32) {
space = true;
break;
}
else {
space = false;
break;
}
}
} while(space);
/*Reset values for the same loop again (I would not like to write 2 times all of this)
i is reseted at loop-for, which helps.*/
space = false;
do {
cout << "MIDDLE NAME: ";
getline(cin, fullName[1]);
for (i = 0; i < fullName[1].size(); i++)
{
if (fullName[i][1] == 32) {
space = true;
break;
}
else {
space = false;
break;
}
}
} while(space);
fullName[3] = fullName[0] + string(" ") + fullName[1] + string(" ") + fullName[2];
}
このループを同じループにまとめることはできません。私は壊れています。
@edit:私は正しいことがfullName [1] [i]を置くことであり、ブレーク条件が間違っているとローガーしています。 edit²@
:結果:
class BankAccount
{
private:
string fullName[5];
char accountAddress[10];
unsigned short int cards;
float money;
bool visa, mastercard, americanExpress;
void checkName(string name, string typeName, bool exception)
{
unsigned short int errorVar, i;
errorVar = i = 0;
bool space = false;
do {
if (errorVar > 0)
cout << "Enter only the name purposed." << endl << endl;
if (exception)
cout << typeName << " NAME (type no if you haven't): ";
else
cout << typeName << " NAME: ";
getline(cin, name);
for (i = 0; i < name.size(); i++)
{
if (name[i] == ' ')
{
space = true;
break;
}
else
space = false;
}
errorVar++;
} while (space);
if (name.compare("no") == 0)
name = "NULL";
}
public:
void createAccount()
{
cout << endl << "FIRST NAME: "; /* First name has no checks (it can be a compound name) */
getline(cin, this->fullName[0]);
checkName(this->fullName[1], "SECOND", false);
checkName(this->fullName[2], "THIRD", true);
checkName(this->fullName[3], "LAST", false);
if (this->fullName[2].compare("NULL") == 0)
{
this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[3]; //NOT FULL NAME
cout << this->fullName[4];
}
else
{
this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[2] + string(" ") + this->fullName[3]; //NOT FULL NAME
cout << this->fullName[4];
}
}
BankAccount() {/* Constructor */}
~BankAccount() {/* Deconstructor */}
};
論理的に、それは簡単なものです。他の分野のコードも手助けして改善してくれてありがとう。
なぜ機能を作成しませんか? – JVApen
いつも壊れたらループを書くのはなぜですか? – JVApen
補足:マジックナンバーを使用しないでください。if(fullName [i] [=] '){' –