#include<iostream> #include<fstream> #include<string> using namespace std; int main() { /*ofstream outfile("grade.txt", ios::out); if (!outfile) { cerr << "error:output file cannot be opened\n"; exit(1); } char id[9], name[16]; int grade; cout << "\t1:"; int n = 1; while (cin>>id>>name>>grade) { if (n==3) { break; } outfile << name << " " << id << " " << grade << endl; cout << "\t" << ++n << ":"; } outfile.close();*/ ifstream infile("grade.txt", ios::in); if (!infile) { cerr << "error:input file cannot be opened.\n"; exit(1); } char id[9], name[16]; int grade, sum = 0, count = 0; while (infile>>id>>name>>grade) { sum += grade; ++count; } infile.close(); cout << "the grade average is:" << float(sum) / count << endl; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter