1. Accept two numbers a,b from the user and print a/b. If b is entered as zero, handle the exception and print appropriate message. Use, try, catch ,throw keywords
2. Accept a month as number (eg: 3 is March), and print month name. Any invalid month should throw an exception, “Month Error”. The program should continuously ask the user to enter the month until the user choses to exit.
3. Read strings from standard input and wite to a file, read the info from the file and print to console.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
try{
int a,b,c;
cout << "enter the integers" << endl;
cin>>a>>b;
cout<<a/b;
if(b==0){
cout<<"Not infinite";
}
else {
throw 505;
}
}
catch(int b){
cout<<" Number greater than zero\n";
}
//question two answer
try{
int n=0;
cout << "enter month number" << endl;
cin>>n;
if(n==1){
cout<<"January"<<endl;
}
else if(n==2)
{
cout<<"February"<<endl;
}
else if(n==3)
{
cout<<"February"<<endl;
}
else if(n==4)
{
cout<<"April"<<endl;
}
else if(n==5)
{
cout<<"May"<<endl;
}
else if(n==6)
{
cout<<"June"<<endl;
}
else if(n==7)
{
cout<<"July"<<endl;
}
else if(n==8)
{
cout<<"August"<<endl;
}
else if(n==9)
{
cout<<"September"<<endl;
}
else if(n==10)
{
cout<<"October"<<endl;
}
else if(n==11)
{
cout<<"November"<<endl;
}
else if(n==12)
{
cout<<"December"<<endl;
}else{
throw "invalid ";
}
}
catch(int n){
cout<<"the month is: "<<n<<endl;
}
//Question three answer
fstream my_file;
my_file.open("my_file",ios::out);
if(!my_file){
cout<<"File not created";
}
else {
cout<<"File created successfully!";
my_file.close();
}
return 0;
}
Comments
Leave a comment