-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathException2(Array-out-of-bounds).cpp
59 lines (55 loc) · 1.42 KB
/
Exception2(Array-out-of-bounds).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* This C++ program demonstrates exception handling and array usage.
* The main function prompts the user to enter five integer elements.
* If the user enters more than five elements, an exception is thrown and caught to display an error message.
* The program successfully handles the exception and displays the entered elements.
*/
// Including Header File
#include <iostream>
// Using Namespace
using namespace std;
// Global Variable
int i = 0;
/**
* @brief The main function demonstrates exception handling and array usage in C++.
*
* This function prompts the user to enter five integer elements. If the user enters more than five elements,
* an exception is thrown and caught to display an error message.
*
* @return 0 upon successful execution.
*/
int main()
{
// Exception Handling
// int a[5]={12,32,12,45,78,89};
/*Exception--
error: too many initializers for 'int [5]'
int a[5]={12,32,12,45,78,89};
^*/
// cout<<a;
try
{
int arr[5];
// for(int i=0;i<=5;i++){
cout << "Enter five elements: " << endl;
while ("%d")
{
if (i == 6)
{
throw i;
}
cout << "Element " << i + 1 << ":";
cin >> arr[i];
i++;
}
for (int i = 0; i < 5; i++)
{
cout << "\t" << arr[i];
}
}
catch (int i)
{
cout << "Too many elements";
}
return 0;
}