In: Computer Science
Create a program that adds 3 assignment marks together and calculate how many more marks they need to pass the course.
To pass the course, you will need to get 50 marks or over.
Output Example
Assignment 1 mark: 10
Assignment 2 mark: 12
Assignment 3 mark: 17
Marks still needed to pass the course: 11
If you input characters, it should print the message Please enter a number and not characters and then ask for that assignment mark again.
(HINT: Use a try-catch block. When you try to convert it to an integer and it fails, the exception should be caught and handled appropriately)
Output Example
Assignment 1 mark: qweqwe2s
Please enter a number and not characters
Assignment 1 mark: asdasd
Please enter a number and not characters
Assignment 1 mark: 12
If you enter anything more than each assignment's maximum marks or a mark less than 0, the program will print: You can't get less than 0 or more than the maximum mark. It will then loop again (HINT) and asks for that assignment mark again.
Output Example
Assignment 1 mark: 12
Assignment 2 mark: 55
You can't get less than 0 or marks more than the maximum mark!
Assignment 2 mark: 13
Assignment 3 mark: -4
You can't get less than 0 or marks more than the maximum mark!
Assignment 3 mark:
Add a stop feature so when the user enters stop, it should print out 'Exited!' and then terminate the program.
Output Example
Assignment 1 mark: 12
Assignment 2 mark: 13
Assignment 3 mark: stop
Exited!
Assume that any valid marks entered for each assignment add up to less than 50.
CODE
#include<iostream>
#include<cstdlib>
#include<stdexcept>
using namespace std;
int main() {
//variables to store marks
string mark1, mark2, mark3;
//variable to store the sum of marks
int sum = 0;
//reading and validating assignment 1 mark
while(1) {
//reading mark 1
cout << "Assignment 1 mark: ";
cin >> mark1;
//checking if the input is stop, if yes exits out of program
if(mark1 == "stop") {
cout << "Exited!" << endl;
exit(1);
}
//try block checking if the input is not a number
try {
if(!stoi(mark1))
throw invalid_argument("Exception");
}
//catch block printing the error
catch(invalid_argument& e) {
cout << "Please enter a number and not characters" << endl;
continue;
}
//checking if input is in the range of mark
if(stoi(mark1) < 0 or stoi(mark1) > 30)
cout << "You can't get less than 0 or more than the maximum marks!" << endl;
else {
//adding mark to sum
sum += stoi(mark1);
break;
}
}
//reading and validating assignment 2 mark
while(1) {
//reading mark 2
cout << "Assignment 2 mark: ";
cin >> mark2;
//checking if the input is stop, if yes exits out of program
if(mark2 == "stop") {
cout << "Exited!" << endl;
exit(1);
}
//try block checking if the input is not a number
try {
if(!stoi(mark2))
throw invalid_argument("Exception");
}
//catch block printing the error
catch(invalid_argument& e) {
cout << "Please enter a number and not characters" << endl;
continue;
}
//checking if input is in the range of mark
if(stoi(mark2) < 0 or stoi(mark2) > 30)
cout << "You can't get less than 0 or more than the maximum marks!" << endl;
else {
//adding mark to sum
sum += stoi(mark2);
break;
}
}
//reading and validating assignment 3 mark
while(1) {
//reading mark 3
cout << "Assignment 3 mark: ";
cin >> mark3;
//checking if the input is stop, if yes exits out of program
if(mark3 == "stop") {
cout << "Exited!" << endl;
exit(1);
}
//try block checking if the input is not a number
try {
if(!stoi(mark3))
throw invalid_argument("Exception");
}
//catch block printing the error
catch(invalid_argument& e) {
cout << "Please enter a number and not characters" << endl;
continue;
}
//checking if input is in the range of mark
if(stoi(mark3) < 0 or stoi(mark3) > 40)
cout << "You can't get less than 0 or more than the maximum marks!" << endl;
else {
//adding mark to sum
sum += stoi(mark3);
break;
}
}
//printing the marks needed to pass
cout << "Marks still needed to pass the course: " << 50 - sum << endl;
return 0;
}
CODE SCREENSHOT
OUTPUT