我的初始化变量显示为空

时间:2013-03-04 19:57:52

标签: c++

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

1 个答案:

答案 0 :(得分: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;
}