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