235k views
2 votes
Write a C++ program to prompt user for a student's full name and 2 test scores and find the average, then print the name, scores and average to the monitor with clear and descriptor labels. Verify data is inputted correctly and test scores must be between 0 and 100. If there was bad data data print an error message and stop the program.

1 Answer

1 vote

Answer:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string name;

int score1, score2 = 0;

double avg = 0;

cout<<"Enter student's name: ";

getline(cin, name);

cout<<"Enter the test score 1: ";

cin >> score1;

cout<<"Enter the test score 2: ";

cin >> score2;

if ((score1 >= 0 && score1 <= 100) && (score2 >= 0 && score2 <= 100)) {

avg = (score1 + score2) / 2.0;

cout << "Name: " << name << endl;

cout << "Test score 1: " << score1 << endl;

cout << "Test score 2: " << score2 << endl;

cout << "Average of the tests: " << avg << endl;

}

else {

cout << "Invalid input!" << endl;

}

return 0;

}

Step-by-step explanation:

Initialize the variables

Ask the user for the name and test scores

Check if the input is in the required range. If it is, calculate the average and print the name, test scores, and the average.

Otherwise, print an error message

User Peterhurford
by
4.5k points