Question

I've built a C++ program that calculates the user's class average based on three test. Can...

I've built a C++ program that calculates the user's class average based on three test. Can somebody show me how to get the program to look for invalid input?

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

int main()

{

//variables

int score_1; //test 1 input from user

int score_2; //test 2 input from user

int score_3; //test 3 input from user

int highest; // the higher score of test 1 & 2

int course_average; //average for class

//Introduce user to program

cout << "Welcome to the grade calculator. You will enter three test scores\n";

cout << "The highest of the first two grades and the third grade will be added together\n";

cout << "to determine the numeric grade average for the course.\n";

cout << "Each test score has a maximum of 50 points.\n\n";

//request input from user

cout << "Enter first test score: ";

cin >> score_1;

cout << "Enter second test score: ";

cin >> score_2;

cout << "Enter third test score: ";

cin >> score_3;

// test to see if user input is valid.

//Calculate numerical average for class

{

if (score_1 > score_2)

(highest = score_1);

else if

(highest = score_2);

}

cout << int(course_average = highest + score_3);

cout << " is your course average. \n";

//define range for letter grade

if ((course_average >= 90) && (course_average < 100))

{

cout << "The letter grade is: " << "A";

}

else if ((course_average >= 80) && (course_average < 90))

{

cout << "The letter grade is: " << "B";

}

else if ((course_average >= 70) && (course_average < 80))

{

cout << "The letter grade is: " << "C";

}

else if ((course_average >= 60) && (course_average < 70))

{

cout << "The letter grade is: " << "D";

}

else if ((course_average >= 50) && (course_average <= 0))

{

cout << "The letter grade is: " << "F";

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

--------------------------------------

Program

//Header files
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Main method
int main(){
   //variables
   int score_1; //test 1 input from user
   int score_2; //test 2 input from user
   int score_3; //test 3 input from user
   int highest; // the higher score of test 1 & 2
   int course_average; //average for class
   //Introduce user to program
   cout << "Welcome to the grade calculator. You will enter three test scores\n";
   cout << "The highest of the first two grades and the third grade will be added together\n";
   cout << "to determine the numeric grade average for the course.\n";
   cout << "Each test score has a maximum of 50 points.\n\n";
   //request input from user
   cout << "Enter first test score: ";
   cin >> score_1;
   while (score_1 < 0 || score_1>100) {
       cout << "ERROR!!!score should be within 0-100!!!" << endl;
       cout << "Enter first test score: ";
       cin >> score_1;
   }
   cout << "Enter second test score: ";
   cin >> score_2;
   while (score_2 < 0 || score_2>100) {
       cout << "ERROR!!!score should be within 0-100!!!" << endl;
       cout << "Enter second test score: ";
       cin >> score_2;
   }
   cout << "Enter third test score: ";
   cin >> score_3;
   while (score_3 < 0 || score_3>100) {
       cout << "ERROR!!!score should be within 0-100!!!" << endl;
       cout << "Enter third test score: ";
       cin >> score_3;
   }
   // test to see if user input is valid.
   if(score_1>=0 && score_2>=0 && score_3>=0 && score_1<=100 && score_2 <=100 && score_3 <= 100)
   {
       //Calculate numerical average for class
       if (score_1 > score_2)

           (highest = score_1);

       else if

           (highest = score_2);

   }
   else {
       cout << "Entered Scores wrong!!!" << endl;
       exit(0);
   }

   cout << int(course_average =( highest + score_3)/2);

   cout << " is your course average. \n";

   //define range for letter grade

   if ((course_average >= 90) && (course_average < 100))

   {

       cout << "The letter grade is: " << "A";

   }

   else if ((course_average >= 80) && (course_average < 90))

   {

       cout << "The letter grade is: " << "B";

   }

   else if ((course_average >= 70) && (course_average < 80))

   {

       cout << "The letter grade is: " << "C";

   }

   else if ((course_average >= 60) && (course_average < 70))

   {

       cout << "The letter grade is: " << "D";

   }

   else if ((course_average >= 50) && (course_average <= 0))

   {

       cout << "The letter grade is: " << "F";

   }

}

-------------------------------------------------

Output

Welcome to the grade calculator. You will enter three test scores
The highest of the first two grades and the third grade will be added together
to determine the numeric grade average for the course.
Each test score has a maximum of 50 points.

Enter first test score: 68
Enter second test score: 75
Enter third test score: 75
75 is your course average.
The letter grade is: C

----------------------------------------------

Note

I assume you want input error check this way, if you want to re-enter if error occur ,then we have to check after each input

Add a comment
Know the answer?
Add Answer to:
I've built a C++ program that calculates the user's class average based on three test. Can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Why is this not giving me a letter grade when run? #include <iostream> #include <iomanip> using...

    Why is this not giving me a letter grade when run? #include <iostream> #include <iomanip> using namespace std; int main() { double grade1, grade2, grade3, grade4, total; cout << "please enter the first grade : "; cin >> grade1; cout << "Please enter the second grade : "; cin >> grade2; cout << "Please enter the third grade : "; cin >> grade3; cout << "Please enter the fourth grade : "; cin >> grade4; total = (grade1 + grade2...

  • Find the errors in following program. //This program uses a switch statement to assign a //...

    Find the errors in following program. //This program uses a switch statement to assign a // letter grade (A, B, C, D, or F) to numeric test score. #include <iostream> uisng namespace std; int main() { int testScore; cout<<"Enter your test score and I will tell you\n"; cout<<"the letter grade you earned: "; cin>>testScore; switch (testScore) { case (testScore < 60) cout<<"Your grade is F.\n"; break; case (testScore <70) cout<<"Your grade is D.\n" break; case (testScore <80) cout<<"Your grade is...

  • Find the errors in following program.   //This program uses a switch statement to assign a...

    Find the errors in following program.   //This program uses a switch statement to assign a // letter grade (A, B, C, D, or F) to numeric test score.     #include <iostream> uisng  namespace std;   int main() {                         int testScore;                         cout<<"Enter your test score and  I will tell you\n";             cout<<"the letter grade you earned: ";             cin>>testScore;               switch (testScore)             {                         case  (testScore < 60)                                     cout<<"Your grade is F.\n";...

  • Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment...

    Microsoft Visual Studios 2017 Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score and total separated by...

  • Professor Dolittle has asked some computer science students to write a program that will help him...

    Professor Dolittle has asked some computer science students to write a program that will help him calculate his final grades. Professor Dolittle gives two midterms and a final exam. Each of these is worth 100 points. In addition, he gives a number of homework assignments during the semester. Each homework assignment is worth 100 points. At the end of the semester, Professor Dolittle wants to calculate the median score on the homework assignments for the semester. He believes that the...

  • /* C++ program that prompts the user to enter the name of the student, enter the...

    /* C++ program that prompts the user to enter the name of the student, enter the five scores values. Then calculate the total scores of the student. Then finally calculate the average score of the total score value. Then, print the name, total score , average scrore value on console output. */ //main.cpp //include header files #include<iostream> #include<string> #include<iomanip> using namespace std; //start of main function int main() {    //declare variable to read string value    string studentName;   ...

  • ASSIGNMENT #3 ** using System; namespace GradeCalculatorNew {    class MainClass    {        public...

    ASSIGNMENT #3 ** using System; namespace GradeCalculatorNew {    class MainClass    {        public static void Main (string[] args)        {            int test1,test2,test3;            double average,average2;            char letterGrade;            Console.WriteLine("Enter test score1");            test1=Convert.ToInt32(Console.ReadLine());            Console.WriteLine("Enter test score2");            test2=Convert.ToInt32(Console.ReadLine());            Console.WriteLine("Enter test score3");            test3=Convert.ToInt32(Console.ReadLine());            average=(test1+test2+test3)/3.0;            average=(int)(average+0.5);           ...

  • Test Scores and Grade Write a program that has variables to hold three test scores. The...

    Test Scores and Grade Write a program that has variables to hold three test scores. The program should ask the user to enter three test scores and then assign the values entered to the variables. The program should display the average of the test scores and the letter grade that is assigned for the test score average. Use the grading scheme in the following table: Test Score Average Letter Grade 90–100 A 80–89 B 70–79 C 60–69 D Below 60...

  • Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score...

    Modify Assignment #18 so it drops each student’s lowest score when determining the students’ test score average and Letter grade. No scores less than 0 or greater than 100 should be accepted #include<iostream> #include<string> using namespace std; int main() { double average[5]; string name[5]; char grade[5]; for(int i=0; i<=5; i++) { cout<<"Enter student name: "; cin.ignore(); getline(cin,name[i]); int sum=0; for(int j=0; j<5; j++) { int grades; cout<<"Enter Grades:"<<j+1<<" : "; cin>>grades; sum+=grades; } average[i]=(float)sum/5; if(average[i]>=90 && average[i]<=100) grade[i]='A'; else if(average[i]>=80...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT