询问数组大小后C ++ Progam崩溃

时间:2019-03-15 07:50:20

标签: c++ arrays size

我正在尝试编写一个程序,您可以控制该数组中的数据。 但是由于某种原因,每次我尝试使用arr.size();时,程序都会崩溃。

#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;

int main() {
    while (true) {
        int choice;

        cout << "Make new Array with custom size...(1)" << endl;
        cout << "Add amount at the end.............(2)" << endl;
        cout << "Delete amount at the end..........(3)" << endl;
        cout << "Add amount at location i..........(4)" << endl;
        cout << "Delete amount at location i.......(5)" << endl;
        cout << "Delete Array......................(6)" << endl;
        cout << "Show Array........................(7)" << endl;
        cout << "END...............................(0)" << endl;
        cout << "Choice: ";
        cin >> choice;

        vector<double> arr;

        switch (choice) {
        case 1: {
            int i;

            cout << "Enter array size: ";
            cin >> i;

            arr.resize(i);

            cout << "Set array size to " << i << "!" << endl;
            cout << "Success!" << endl;

            system("pause");
            system("cls");

            break;
        }

        case 2: {
            double amount;

            cout << "Enter amount: ";
            cin >> amount;

            int location = arr.size();
            cout << location << " " << amount << endl;

            arr[location] = amount;

            cout << arr[location] << endl;
            cout << "Successfully saved!" << endl;

            system("pause");
            system("cls");
            break;
        }

        case 3:

            arr[arr.size()] = 0;

            cout << "Success: " << arr[arr.size] << endl;

            system("pause");
            system("cls");

            break;

        case 4: {
            int ite;
            float numb;

            cout << "Please enter amount: ";
            cin >> numb;
            cout << "Please enter amount for the i'th location: ";
            cin >> ite;

            cout << "Success!" << endl;

            system("pause");
            system("cls");

            break;
        }

        case 5:
            int ites;

            cout << "Please enter amount for the i'th location: ";
            cin >> ites;

            arr[ites] = 0;

            cout << "Success!" << endl;

            system("pause");
            system("cls");

            break;

        case 6:
            int o;

            for (o = 0; o < arr.size(); o++) {
                arr[o] = 0;
            }

            cout << "Success!" << endl;
            system("pause");
            system("cls");

            break;

        case 7:
            int j;

            for (j = 0; j < ARRAYSIZE(arr); j++) {
                cout << "Array[" << j << "] = " << arr[j] << endl;
            }

            system("pause");
            system("cls");

            break;

        case 0: {
            cout << "Exit Program....";

            return 0;
        }
        }
    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

此行

for(j = 0; j < ARRAYSIZE(arr); j++){

什么是ARRAYSIZE?它不在代码中的任何地方定义。您可能是这样说的:

for(j = 0; j < arr.size(); j++){

另外,请注意size是一个函数,因此您需要通过将()放在末尾来调用它。这在这里行不通:

cout << "Success: " << arr[arr.size] << endl;

您需要arr.size()进行编译,即使那样,这也超出了范围。要打印最后一个元素,请执行arr[arr.size()-1],或者执行更好的操作,arr.back()(并使用arr.empty()确保数组不为空。)要仅打印数组的大小,请执行以下操作:

cout << "Success: " << arr.size() << endl;

注意一点(不用担心,它不会引起任何问题):在此循环中

int o;
    for(o = 0; o < arr.size(); o++){
        arr[o] = 0;
    }

由于您不在循环外使用o,因此可以将声明移入循环初始化中,如下所示:

for(int o = 0; o < arr.size(); o++){
    arr[o] = 0;
}

如果您在此处收到有关有符号/无符号不匹配的警告,则可以通过将int更改为unsigned int来消除它。

答案 1 :(得分:1)

第83行(案例3) 你忘了括号 你写了

cout << "Success: " << arr[arr.size] << endl;

如果要调用函数size()来写出大小,则应为

cout << "Success: " << arr[arr.size()] << endl;