我是C ++的初学者。
我应该制作输出形状区域的程序。
例如,
Input 1. Square Input 2. Rectangle Input 3. Circle Input 4. Print all the areas User input: 1 Please give me the side. The side is: 3 User input: 4 Square area: 9 //////////////////////////////////////////////////////
但是存在引发异常的问题。
我不知道为什么编译器会指出这一点。
有人可以解释吗?
source.cpp:
using namespace std;
int main() {
const int n = 10;
Shape * arr[n];
int input, index = 0;
do
{
cout << "What shape that you want to create?" << endl;
cout << "Input 1. Square \n";
cout << "Input 2. Rectangle \n";
cout << "Input 3. Circle \n";
cout << "Input 4. Print all the areas \n";
cout << "Input -1. Exit \n";
cout << "User input: ";
cin >> input;
//Use switch statement to check user's input
switch (input)
{
case -1:
cout << "You inputted number : -1 \n" << "The program will be ended. " << endl;
break;
case 1:
double side;
cout << "Please give me the side: ";
cin >> side;
arr[index] = new Square(side);
index++;
break;
case 2:
double width, length;
cout << "Please give me the width and length \n";
cout << "Width is: ";
cin >> width;
cout << "Length is: ";
cin >> length;
arr[index] = new Rectangle(width, length);
index++;
break;
case 3:
double radius;
cout << "Please give me the radius: ";
cin >> radius;
arr[index] = new Circle(radius);
index++;
break;
case 4:
for (int i = 0; i < 10; i++)
{
cout << arr[i]->getName();
cout << " area: ";
cout << fixed << showpoint <<
setprecision(2);
cout << arr[i]->area() << endl;
}
break;
default:
cout << "You entered different number mentioned above!" << endl;
}
} while (input != -1);
delete[] arr;
system("pause");
return 0;
}
编译器显示Project.exe中的0x0130C213引发了异常:0xC0000005:访问冲突读取位置0xCCCCCCCC。
我不明白为什么编译器会显示这个。
请给我任何建议!