-1

我的字符串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;
}
4

1 回答 1

1

您允许用户输入最多 100 个字符串,但您的数组最多只能容纳 99 个字符串。如果他们实际上输入了 100 个字符串,最后一个字符串将损坏内存,假设您的代码没有完全崩溃。

此外,您的字母循环中有一些错误的逻辑。if (counter < user_Input)将始终为真,因此coun将始终如此,因此在到达数组末尾counter+1时会超出数组的边界。counter此外,您的循环对于他们试图做的事情来说是不必要的复杂。

试试这个:

int main()
{
    string user_String[100];
    int user_Input;

    do
    {
        cout << "Please enter the number of Strings (1-100): ";
        if (cin >> user_Input)
        {
            if ((user_Input >= 1) && (user_Input <= 100))
                break;
        }
        else
            cin.clear();
    }
    while (true);

    for (int counter = 0; counter < user_Input; ++counter)
    {
        cout << "Enter String " << counter + 1 << ": ";
        cin >> user_String[counter];
    }

    string b = user_String[0]; 
    for(int counter = 1; counter < user_Input; ++counter)
    {
        string a = user_String[counter];
        if (a.length() > b.length())
            b = a;
    }

    cout << "The string " << b << " has the most letters." << endl;

    b = user_String[0]; 
    for(int counter = 1; counter < user_Input; ++counter)
    {
        string a = user_String[counter];
        if (a.length() < b.length())
            b = a;
    }

    cout << "The string " << b <<" has the least letters." << endl;

    system("pause");
    return 0;
}

话虽如此,您可以完全摆脱数组并将循环合并在一起:

int main()
{
    string user_String;
    int user_Input;

    do
    {
        cout << "Please enter the number of Strings: ";
        if (cin >> user_Input)
        {
            if (user_Input >= 1)
                break;
        }
        else
            cin.clear();
    }
    while (true);

    string fewestLetters, mostLetters;
    for (int counter = 1; counter <= user_Input; ++counter)
    {
        cout << "Enter String " << counter << ": ";
        cin >> user_String;

        if (counter == 1)
        {
            mostLetters = user_String;
            fewestLetters = user_String;
        }
        else
        {
            if (user_String.length() > mostLetters.length())
                mostLetters = user_String;

            if (user_String.length() < fewestLetters.length())
                fewestLetters = user_String;
        }
    }

    cout << "The string " << mostLetters << " has the most letters." << endl;
    cout << "The string " << fewestLetters << " has the least letters." << endl;

    system("pause");
    return 0;
}
于 2013-03-04T20:14:49.320 回答