In: Computer Science
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
Program:
#include <iostream>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
srand(time(0)); //seed random number generator
int scores[20],a; // variable declaration
ifstream infile; // input file declaration
ofstream outFile("scores.txt"); // output file declaration
infile.open("scores.txt"); // Syntax for open the file
for(int i=0;i<20;i++)
{
scores[i] = rand() %11; // random number between 0 and 10
outFile <<scores[i]<<endl; // write the scores into the file
}
cout<<"Double the scores in the file:"<<endl;
while (infile >> a) // read the file content
{
int doubleScore=2*a;
cout<<doubleScore<<" "; // store the values in file
}
infile.close(); // close the file
cout<<endl;
return 0;
}
Output:
Any doubts leave a comment
Please Rate My Answer