C 程序要求数组的大小后崩溃
C++ Program crashes after asking for the size of an array
我正在尝试编写一个程序,您可以在该程序中控制该数组中的数据。但是由于某种原因,每次我尝试使用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;
相关文章:
- 类对象数组的问题会导致崩溃
- 字节数组初始化会导致 DirectX 崩溃
- 使用 sf::Text 数组时崩溃
- For 循环在尝试读取数组 c++ 时程序崩溃
- 删除数组 c++ 崩溃问题
- 如果类的成员,C++数组会导致崩溃
- 在C++中删除指针数组时析构函数崩溃
- 在 C++ 中使用平展数组语法,崩溃取决于宽度/高度
- 我的单例中的数组在离开函数后没有保留信息,然后在尝试再次访问信息时崩溃
- C++ rezing 动态数组最多可以处理一定数量的元素,但在某些时候会崩溃并显示错误 (0XC0000005)
- 释放字符的动态数组时崩溃
- size_t结构非数组成员崩溃的强制转换
- C++程序在迭代到数组的最后一个元素时崩溃
- 从数组比较2个字符串时,程序崩溃
- C++ 尝试向类内的数组(或向量)添加值时程序崩溃
- 内存相关崩溃:Cocos2d游戏中的三维数组
- 尝试获取数组中的最小元素时崩溃
- 动态数组在构造函数处崩溃
- 使用数组的单向链表:它在运行时崩溃
- C 程序崩溃(数组和2个指针Binsearch功能)