I'm a beginner in C++.
I should make program that outputs shape's area.
For example,
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 //////////////////////////////////////////////////////
But there is problem about exception thrown.
I don't know why compiler indicates this.
Can someone explain this?
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;
}
The compiler shows that Exception thrown at 0x0130C213 in Project.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
I don't understand why the compiler shows this.
Please give me any advice!
User contributions licensed under CC BY-SA 3.0