read the program; it looks a lot like warm up 2, right? instead of reading data from a text file to your program, you'll be inputting numbers into an array, then copying those numbers to a file created by your .cpp program after running the program, look for (and hopefully find) the text file (should be in the same folder as your.cpp file) then, add the following to the program: o input validation, so that only valid quiz scores are entered (not less than zero nor greater than 100) • statements that will compute the average quiz score and write the average, in a sentence that says "Your average score is save and run the program; confirm that input validation works, and the average sentence appears at the end of your myData.txt file
answer in c++
Solution:
In the code that is given in the question :
- input validation can be performed as follows :
// performing input validation to check if the score <=0 and score>100
while (scores[count] <= 0 || scores[count] > 100)
{
cout << "value not accepted.\n";
cin >> scores[count];
}
- The average of quiz scores can be done as follows:
sum= sum + scores[count];
avg = sum /quizzes;
// code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ const int quizzes =5;
int scores[quizzes];
int count =0;
int sum=0;
double avg = 0.0;
ofstream outputfile;
if (scores == NULL)
return 0;
//Get the quizz score for each test
cout << "Enter the quizz scores below.\n";
for (int count = 0; count <quizzes ; count++)
{
cin >> scores[count];
// performing input validation to check if the score <=0 and score>100
while (scores[count] <= 0 || scores[count] > 100)
{
cout << "value not accepted.\n";
cin >> scores[count];
}
// calculating the average of quiz scores
sum= sum + scores[count];
avg = sum /quizzes;
}
outputfile.open("myData.txt");
for(count=0;count<quizzes;count++)
outputfile <<scores[count] << endl;
outputfile <<"your average score is " <<avg <<endl;
outputfile.close();
cout<<" The data was saved to file";
cout<< "\n your average score is"<<avg<<endl;
return 0;
}
Step by step
Solved in 3 steps with 2 images