Question #75745

A text file data.txt, contains at most 80 student marks in an exam. Each mark is out of 100. The last number in the file is -1 indicating that there is no more data in the file.

read all the marks in the data file and store in an array, print all the elements in the array

Expert's answer

#include <iostream>
#include<array>
#include<fstream>
#include<ctime>
int main()
{
std::array<int,80> _marks;

std::ifstream _read("data.txt");
int i = 0;
int _getnumber = 0;
while (_read>>_getnumber)
{

if (_getnumber==-1) break;
_marks[i] = _getnumber;
i++;
}
for (auto i:_marks)
std::cout<<i<<' ';
return 0;
}


data.txt:
45 7 19 48 10 9 27 30 69 93 27 12 20 77 35 97 21 9 62 2 15 69 35 44 94 72 67 79 30 44 38 35 62 35 32 10 21 41 26 22 90 55 41 68 17 28 82 45 54 18 19 32 92 87 21 81 32 21 42 53 3 18 77 37 24 32 47 71 91 23 29 22 39 64 95 81 15 3 79 36 -1
LATEST TUTORIALS
APPROVED BY CLIENTS