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...
Assignment #3 – The Accounting Cycle in Review This assignment is out of 101 marks and...
Assignment #3 – The Accounting Cycle in Review This assignment is out of 101 marks and is weighted 15% of your final grade. Comprehensive Question- Jerry’s Mobile Car Wash Jerry Fish opened Jerry’s Mobile Car Wash on Sept 1, 2017. In Sept, the following transactions were completed. Sept 1 Invested $14,000 cash in the business 1 Purchased a used truck for $26,400, paying $6,400 cash and signing a note payable for the balance 1 Collected $3,000 from XYZ for their...
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...
*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices...
*****In C++***** Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...
You have been asked to create a python program that will ask the user how many...
You have been asked to create a python program that will ask the user how many tickets the user would like to purchase for graduation. The user will then choose the number of tickets needed and the program will list the cost of the tickets, a service fee, tax, and the total of the purchase. Specifications: The program should only accept whole numbers when receiving input for the number of tickets needed. No calculations should be performed if input is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT