C 程序要求数组的大小后崩溃

C++ Program crashes after asking for the size of an array

本文关键字:崩溃 数组 程序      更新时间:2023-10-16

我正在尝试编写一个程序,您可以在该程序中控制该数组中的数据。但是由于某种原因,每次我尝试使用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;
}

在此行上

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

在第83号线(内部案例3)

您忘记了括号

你已经写了

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

如果要调用将返回大小的函数size(),则应该是:

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