In: Computer Science
n-class: Write a program to store exam scores into a file. The program will ask the user how many exam scores to enter. Then it will ask the user for each exam score and store them in a file called scores.dat The file output should have the following format . Exam 1: 97 Exam 2: 85
C++ Coding please
The code for the above problem is:-
#include<iostream>
using namespace std;
#include<fstream>
main(){
int limit,score; // variables to store the number of scores and the
value of each score,respectively
cout<<"How many exam scores do you want to enter? \n";
//prompting the user for the number of scores he/she wants to
enter
cin>>limit; //storing the number of exams for which the use
wants to enter the value
cout<<"please enter the scores\n"; //prompting the user to
enter the scores foe each exam
ofstream myfile; // declaring a pointer(myfile) to the file
myfile.open("scores.dat"); //opening a file named scores.dat
for(int i=1;i<=limit;i++) //loop for storing the values entered
by the user into the file
{
cin>>score; //storing the values into the
variable
myfile<<"Exam "<<i<<":
"<<score<<"\n"; // storing the values into the file as
per desired format
}
myfile.close(); //closing the file
}
The Screenshots of the code,output and the stored values in the file are given below:-
Code:-
Output screen:-
Store values in the file:-
Note:- The file "Scores.dat" will be generated in the location in which you save your code.