私の文字列d
はコンソールに空(スペースさえも)で表示されます。これは、文字列を「NULL」に初期化し、空の値ではない新しい値を割り当てようとしているため、混乱しています。
int main(){
string user_String[99];
int user_Input = 0;
cout << "Please insert up to one hundred Strings: ";
cin >> user_Input;
//Check for range
bool check = false;
while( check == false){
if (user_Input < 1 || user_Input >100){
cout << "Please insert up to one hundred Strings: ";
cin >> user_Input;}
else{
check = true;
break;}
}
//User input
cout <<"Please enter string"<< endl;
for (int counter = 0; counter < user_Input; counter++){
int counter2 = counter + 1;
cout << "Enter String " << counter2 << ": ";
cin >> user_String[counter];
}
//Loopig for most letters
string c = "NULL";
for(int counter = 0; counter < user_Input; counter++){
//Making Sure Coun doesn't go out of range
int coun = 0;
if (counter < user_Input){
coun = counter +1;}
else{
coun = counter;
}
string a = user_String[counter];
string b = user_String[coun];
if (a.length() < b.length() && c == "NULL"){
c = b;
}
if(a.length() < b.length() && c!="NULL" && c.length() < b.length()){
c = b;
}
else{
continue;
}
}
cout << "The string "<< c <<" have the most letters." << endl;
//Looping for least letters
string d = "NULL";
for(int counter = 0; counter < user_Input; counter++){
//Making Sure Coun doesn't go out of range
int coun = 0;
if (counter < user_Input){
coun = counter +1;}
else{
coun = counter;
}
string a = user_String[counter];
string b = user_String[coun];
if (a.length() > b.length() && d == "NULL"){
d = b;
}
if(a.length() > b.length() && d!="NULL" && d.length() > b.length()){
d = b;
}
else{
continue;
}
}
cout << "The string " << d <<" have the least letters." << endl;
system("pause");
return 0;
}