12.6 lab: exceptions with vectors complete a program that reads a vector index as input and outputs the element of a vector of 10 names at the index specified by the input. use a try block to output the name and a catch block to catch any out of range exceptions. when an out of range exception is caught, output the message from the exception object and the first element in the vector if the index is negative or the last element if the index is greater than the size of the vector. hint: format the exception outputs using the what() function from the exception object. do not hard code the exception messages. ex: if the input of the program is:

Respuesta :

The program that reads a vector index as input and outputs the element of a vector will be:

#include<iostream>

#include<vector>

#include<stdexcept>

using namespace std;

int main(){

   // set vector of names

   vector<string> names = {"Ryley","Edan","Reagan","Henry","Caius","Jane","Guto","Sonya","Tyrese","Johny"};

   // get index

   int index;

   cin>>index;

   // size of the names

   int indexLength = names.size();

   // inside try

   try {

       // if at fixed length

       cout<<names.at(index);

   }

   // otherwise throw exception

   catch(const std::out_of_range& oor){

       cout << "Exception! " << oor.what() << '\n';

       // if index is greater or equal to index

       if(index>=indexLength){

           cout<<"The closest name is: "<<names.at(indexLength-1)<<endl;

       }

       // otherwise

       else {

           cout<<"The closest name is: "<<names.at(0)<<endl;

       }

return 0;

How is the program illustrated?

The program segment is an illustration of the try and catch program exceptions. It should be noted that the program segment written in C++, where comments are used to explain each action.

In this case, the program that reads a vector index as input and outputs the element of a vector of 10 names at the index specified by the input.

Learn more about program on:

https://brainly.com/question/27037407

#SPJ1