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
|
//This program reads 10 names and 5 grades from various files and averages them.
#include <iostream>
#include <fstream>
#include <string>
using namespace std; // to prevent having to use std:: every time we use certain functions
int main() // main function
{
ifstream nam, grd;
ofstream aver; // this will be the output file's stream
int grd1, grd2, grd3, grd4, grd5; //Holds the sums of the 5 grades
string nam1, nam2, nam3, nam4, nam5, nam6, nam7, nam8, nam9, nam10; //These are variables that hold the name strings
int grade [9] [4]; // I decided to try and use an array to hold the grade values
cout<< "Welcome to the grade averager.\n";
nam.open("C:\\Users\\Alex\\Desktop\\names.txt");
cout <<"Opening names.txt\n";
if (!nam)
{
cout << "Error Opening File. Sorry...\n";
return 0;
}
nam >> nam1 >> nam2>> nam3 >> nam4 >> nam5
>> nam6 >> nam7 >> nam8 >> nam9 >> nam10; // Assigns the names in the file "names.txt" to a variable accesible in
// the program.
nam.close();
cout << "The names have been loaded. \n"; // close the stream
cout << "Press ENTER to continue..\n";
cin.get();
grd.open("C:\\Users\\Alex\\Desktop\\grades.txt"); // Open the file with the sets of grades in them
cout << "Attemtping to open grades.txt\n";
if(!grd)
{
cout << "Was unable to locate ""grades.txt""\n"; // makes sure that grades.txt was successfully opened.
return 0;
}
grd >> grade [0] [0] >> grade [0] [1] >> grade [0] [2] >> grade [0] [3] >> grade [0] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]
>> grade [1] [0] >> grade [1] [1] >> grade [1] [2] >> grade [1] [3]>> grade [1] [4]; //load all the grades from the file. I read that
//you are only allowed to assign one array at a time.
// I am unsure if there is a faster way of doing this
grd1= ((grade [0] [0] + grade [0] [1] + grade [0] [2] + grade [0] [3] + grade [0] [4])/5); //Adds then divides, assigns to grd1
cout << nam1<< "'s grade is ["<< grd1<< "]" << endl;
grd2= (( grade [1] [0] + grade [1] [1] + grade [1] [2] + grade [1] [3]+ grade [1] [4])/5);
cout << nam2<< "'s grade is ["<< grd2<< "]" << endl;
grd3= (( grade [2] [0] + grade [2] [1] + grade [2] [2] + grade [2] [3]+ grade [2] [4])/5);
cout << nam3<< "'s grade is ["<< grd3<< "]" << endl;
grd4= (( grade [3] [0] + grade [3] [1] + grade [3] [2] + grade [3] [3]+ grade [3] [4])/5);
cout << nam4<< "'s grade is ["<< grd4<< "]" << endl;
grd5= (( grade [4] [0] + grade [4] [1] + grade [4] [2] + grade [4] [3]+ grade [4] [4])/5);
cout << nam5<< "'s grade is ["<< grd5<< "]" << endl;
cout<< grade [0] [4];
cout << "Press ENTER to end.";
cin.get();
return 0;
}
|