Question

In: Computer Science

Create a program that adds 3 assignment marks together and calculate how many more marks they...

Create a program that adds 3 assignment marks together and calculate how many more marks they need to pass the course.

  • Assignment 1 has a total of 30 marks
  • Assignment 2 has a total of 30 marks
  • Assignment 3 has a total of 40 marks

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.

Solutions

Expert Solution

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


Related Solutions

Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
3. Capital Budgeting Assignment (15 marks) Instructions: The assignment is based on the case below. The...
3. Capital Budgeting Assignment Instructions: The assignment is based on the case below. The instructions relating to the assignment are at the end of the case. Dan and Susan are facing an important decision. After having discussed different financial scenarios, the two computer engineers felt it was time to finalize their cash flow projections and move to the next stage – decide which of two possible projects they should undertake. Both had a bachelor degree in engineering and had put...
In java Create a program to calculate interest rate and total balance - the program consists...
In java Create a program to calculate interest rate and total balance - the program consists of (a) A class that encapsulates the interest rate and total balance calculation Constructor should accept initial balance and interest rate Methods: Returns interest rate Returns total balance Set interest rate Set initial balance (b) A test program that uses class from step (A) to perform the following: Set interest rate to 5% and initial balance to 1000 Print interest amount and total balance...
Overview For this assignment, write a program that will calculate the quiz average for a student...
Overview For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT