Example:4
Write a C++ program to take input for student details like rolll number ,name , marks of three subject (m1,m2,m3). Calculate and print total and per.
Condition:
1. roll no should be a three digit no
2. marks of m1 should be >=0 and <=100
3. marks of m2 should be >=0 and <=100
4. marks of m3 should be >=0 and <=100
Sol:
#include <iostream> using namespace std; class roll_error { }; class m1_error { }; class m2_error { }; class m3_error { }; void check_roll(int num) { if (num<100 || num>999) throw roll_error(); } void check_m1(float n) { if (n<0 || n>100) throw m1_error(); } void check_m2(float n) { if (n<0 || n>100) throw m2_error(); } void check_m3(float n) { if (n<0 || n>100) throw m3_error(); } int main() { int roll; char name[20]; float m1,m2,m3,total,per; try { cout<<" enter roll number "; cin>>roll; cout<<"Enter name "; cin>>name; cout<<"Enter marks of m1,m2 and m3 "; cin>>m1>>m2>>m3; check_roll(roll); check_m1(m1); check_m2(m2); check_m3(m3); cout<<"roll = "<<roll<<endl; cout<<"Name = "<<name<<endl; cout<<"m1 = "<<m1<<endl; cout<<"m2 = "<<m2<<endl; cout<<"m3 = "<<m3<<endl; total=m1+m2+m3; per=total/3; cout<<"total "<<total<<endl; cout<<"per "<<per<<endl; } catch(roll_error) { cout<<" Roll number has to be a 3 digit number"<<endl; } catch(m1_error) { cout<<" m1 marks invalid"<<endl; } catch(m2_error) { cout<<" m2 marks invalid"<<endl; } catch(m3_error) { cout<<" m3 marks invalid"<<endl; } return 0; }