1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
string name, social, address, telephone, course, header;
int n, age, years;
double test;
double total;
double average;
const int NUMBER_OF_COURSES = 3;
const int NUMBER_OF_SCORES = 5;
int min_students = 1,
max_students = 100;
//number of students
cout << "Enter how many students do you want to see:" << endl;
cin >> n;
// Validate the input while loop
while (n < min_students || n > max_students )
{
cout << "You should have at least "<< min_students << " student"
<< " and more than " << max_students << " students" << endl;
//get number of students again
cout << "Enter how many students do you want to see:";
cin >> n;
}
//Open Input and Output file
ifstream fin;
fin.open("Input.txt");
if (!fin) {
cout << "could not open file" << endl;
fin.ignore();
return -1;
}
ofstream fout;
fout.open("Output.txt");
if (!fout) {
cout << "could not open file" << endl;
return -1;
}
// Ask for some info
for (int i = 0; i < n; ++i) {
getline(fin,name);
fin >> age;
fin.ignore();
getline(fin,address);
fin >> years;
fin.ignore();
getline(fin,telephone);
getline(fin,social);
fout << name << endl;
fout << age << endl;
fout << address << endl;
fout << years << endl;
fout << telephone << endl;
fout << social << endl;
for (int j = 0; j < NUMBER_OF_COURSES; ++j) {
fin.ignore();
getline(fin,course);
fout << course << endl;
for (int k = 0; k < NUMBER_OF_SCORES; k++) {
fin >> test;
total = total + test;
fin.ignore();
fout << test << endl;
}
average = total / NUMBER_OF_SCORES;
fout << average << endl;
}
}
cout << "Data written to output file." << endl;
fin.close();
fout.close();
return 0;
}
|