在询问数组的大小后,C ++程序崩溃了

问题描述 投票:1回答:2

我正在尝试编写一个程序,你可以控制该数组中的数据。但由于某些原因,每次我尝试使用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;
}
c++ arrays size
2个回答
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
投票

在第83行(在案例3中)你忘记了你写的括号

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

如果你想调用函数size(),它将写入它应该的大小

cout << "Success: " << arr[arr.size()] << endl;
© www.soinside.com 2019 - 2024. All rights reserved.