Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in which main() calls function f1() in a try block. Keep in mind

ID: 3881572 • Letter: W

Question

Write a program in which main() calls function f1() in a try block. Keep in mind exceptions and the name of the include file

void f1(std::string s) {

/* if s is empty, throws a std::domain_error. Else, prints s to console. Always prints std::cout << " found." after the else-body, right before return;*/}

main(){

/*Has a try block that calls f1 four times, with "Your name", "Friend's name", "", and "Bob" */

/* Also catches std::domain_error e and prints "Name refused." */

/* After catch block, the program prints seven equals-signs on a new line */

/* =======*/

}

Explanation / Answer

#include <iostream>
#include<cstring>
using namespace std;


void f1(string s)
{
   int i=0;
   for(i=0;s[i]!='';i++);
   if(i==0)//string is empty
   {
       throw "Domain error ";
   }
   else
   {//printing string
   cout<<"found :"<<s<<endl;
      
   }
   return;
}

int main () {
  
string s;

try {
//function calling
f1(s);
} catch (const char* msg) {
cerr << msg << endl;
}

return 0;
}

output:

Domain error

Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote