I need some help trying to code a program. In my assignment I had to create the
ID: 3564694 • Letter: I
Question
I need some help trying to code a program. In my assignment I had to create the code for a program to show how a teacher could input test scores and have them averaged I got this completed and then in part B it wants me to code the program where if the teacher was to enter the wrong score (greater than 100) that if would ask the user if this was correct. The program will not add a score that is more than 100 points to the accumulator without the teachers permission. Below is what I have so far but I cannot figure out how to do the yes or no question. I'm assuming I need to declare variable such as; char answer ' '; and also add another statement somewhere but I'm not sure where. I've had it everywhere and it hasn't worked right.
#include <iostream>
using namespace std;
int main()
{
//declare variables
int score = 0;
int totalPoints = 0; //accumulator
char grade = ' ';
//get first score
cout << "First score (-1 to stop): ";
cin >> score;
while (score != -1)
{
//update accumulator
//then get another score
totalPoints += score;
cout << "Next score (-1 to stop): ";
cin >> score;
} //end while
//determine grade
if (totalPoints >= 315)
grade = 'A' ;
else if (totalPoints >= 280)
grade = 'B';
else if (totalPoints >= 245)
grade = 'C';
else if (totalPoints >= 210)
grade = 'D';
else
grade = 'F';
//end if
//display the total points and grade
cout << "Total points earned: "
<< totalPoints << endl;
cout << "Grade: " << grade << endl;
//system("pause")
return 0;
} //end of main function
Thanks
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
//declare variables
int score = 0;
int totalPoints = 0; //accumulator
char grade = ' ';
// Input char defined
char option;
//get first score
cout << "First score (-1 to stop): ";
cin >> score;
while (score != -1)
{
//Checking value of score and if greater than 100 , ask from user
if(score>100)
{
cout<<"Input Marks is greater than 100.Do you want to add this marks:y/n ";
cin >> option;
}
else{
// If score less than 100 , then option is y
option='y';
}
// If option is y, add a score that is more than 100 points to the accumulator with the teachers permission
if(option=='y')
{
totalPoints += score;
}
//then get another score
cout << "Next score (-1 to stop): ";
cin >> score;
} //end while
//determine grade
if (totalPoints >= 315)
grade = 'A' ;
else if (totalPoints >= 280)
grade = 'B';
else if (totalPoints >= 245)
grade = 'C';
else if (totalPoints >= 210)
grade = 'D';
else
grade = 'F';
//end if
//display the total points and grade
cout << "Total points earned: "
<< totalPoints << endl;
cout << "Grade: " << grade << endl;
//system("pause")
return 0;
} //end of main function