如何确定阵列的大小?

时间:2022-06-27 07:22:17

So I'm trying to have the user enter strings that will be put into an array with the maximum size of 50 strings. Whenever the user enters "stop", I want the loop to stop and the array to cutoff there.

所以我试图让用户输入将放入最大大小为50个字符串的数组中的字符串。每当用户输入“停止”时,我希望循环停止并且阵列在那里切断。

I then want to read the array back to the user. I tried running this and got a really weird error. Can someone explain to me why this doesn't work and how I would fix it? Thanks.

然后我想将数组读回给用户。我尝试运行它并得到一个非常奇怪的错误。有人可以向我解释为什么这不起作用以及我如何解决它?谢谢。

int main(int argc, const char * argv[]){

    string array[50];

    // Get's inputs for array
    for(int i = 0; i < 50; i++){
        cout << "Enter string: ";
        getline(cin, array[i]);

        if(array[i]== "stop"){
            array[i] = "\0";
            break;
        }

     }

    // Reads inputs from array
    for(int i = 0; i < sizeof(array); i++){
        cout << array[i] << "\n";
    }

    return 0;


}

I don't know why I'm getting down voted so hard?

我不知道为什么我这么努力投票?

3 个解决方案

#1


Use std::vector for your task. It has the method push_back() where you can add elements dynamically. Also you can check the size of the vector easily by calling vector.size().

使用std :: vector完成任务。它有方法push_back(),您可以在其中动态添加元素。您还可以通过调用vector.size()轻松检查向量的大小。

int main(int argc, const char * argv[]){

    vector<std::string> array;

    // Get's inputs for array
    for(int i = 0; i < 50; i++){
        cout << "Enter string: ";
        string line;
        std::getline(cin, line);
        array.push_back(line);

        if(array[i]== "stop"){
            array[i] = "\0";
            break;
        }

     }

    // Reads inputs from array
    for(int i = 0; i < array.size(); i++){
        cout << array[i] << "\n";
    }

    return 0;
}

In your code you would have to implement a counter that counts, how many inputs the user did.

在您的代码中,您必须实现一个计数器,用户执行了多少输入。

#2


Normally you would use vectors, but since you have not reached that section of the book, I guess they want you to solve it with C-style arrays.

通常你会使用向量,但由于你没有达到本书的那一部分,我想他们希望你用C风格的数组来解决它。

You can get the length of a C-style array like this

你可以得到像这样的C风格数组的长度

int size = (sizeof(array)/sizeof(*array)

But since you know the maximum number of entries are 50 and that you must stop when you reach the word stop, maybe you rather want this

但是既然你知道条目的最大数量是50,那么当你到达单词stop时你必须停止,也许你想要这个

for(int i = 0; i < 50; i++){
    if (array[i] == '\0')
        break;
    cout << array[i] << "\n";
}

#3


I just implemented a loop to find out how long the array is.

我刚刚实现了一个循环来找出数组的长度。

// Determines how long the array is
    for(int i = 0; i < 50; i++){

        if(array[i] == "\0"){
            break;
        }

        counter++;
    }

#1


Use std::vector for your task. It has the method push_back() where you can add elements dynamically. Also you can check the size of the vector easily by calling vector.size().

使用std :: vector完成任务。它有方法push_back(),您可以在其中动态添加元素。您还可以通过调用vector.size()轻松检查向量的大小。

int main(int argc, const char * argv[]){

    vector<std::string> array;

    // Get's inputs for array
    for(int i = 0; i < 50; i++){
        cout << "Enter string: ";
        string line;
        std::getline(cin, line);
        array.push_back(line);

        if(array[i]== "stop"){
            array[i] = "\0";
            break;
        }

     }

    // Reads inputs from array
    for(int i = 0; i < array.size(); i++){
        cout << array[i] << "\n";
    }

    return 0;
}

In your code you would have to implement a counter that counts, how many inputs the user did.

在您的代码中,您必须实现一个计数器,用户执行了多少输入。

#2


Normally you would use vectors, but since you have not reached that section of the book, I guess they want you to solve it with C-style arrays.

通常你会使用向量,但由于你没有达到本书的那一部分,我想他们希望你用C风格的数组来解决它。

You can get the length of a C-style array like this

你可以得到像这样的C风格数组的长度

int size = (sizeof(array)/sizeof(*array)

But since you know the maximum number of entries are 50 and that you must stop when you reach the word stop, maybe you rather want this

但是既然你知道条目的最大数量是50,那么当你到达单词stop时你必须停止,也许你想要这个

for(int i = 0; i < 50; i++){
    if (array[i] == '\0')
        break;
    cout << array[i] << "\n";
}

#3


I just implemented a loop to find out how long the array is.

我刚刚实现了一个循环来找出数组的长度。

// Determines how long the array is
    for(int i = 0; i < 50; i++){

        if(array[i] == "\0"){
            break;
        }

        counter++;
    }