Sunday, September 13, 2015

Sum and Average of a Number Using Text File in C++

In this article I would like to share with you a sample program on how to store values in a text file using C++ this activity I wrote during my work as a college instructor in my programming class in C++. I called this program Sum and Average of a Number Using Text file in C++.  All my programming activities that I wrote I am using Dev C++ or CodeBlock as my text editor. I hope you will find my work useful.

If you  have some questions please send me an email at jake.r.pomperada@gmail.comand jakerpomperada@yahoo.com. My mobile number here in the Philippines is 09173084360.

Program Listing

#include <iostream>
#include <fstream>

using namespace std;

main() {

     int val[5], sum=0, average=0,list=0;
     ofstream file("result.txt");
     cout << "\t\tSUM AND AVERAGE OF THE NUMBER 1.0";
     cout << "\n\n";
     for ( list=0; list < 5; list++) {
      cout << "Enter Item No. " << list+1 << " : ";
      cin >> list[val];
     }
     cout << "\n";
     cout << "\n List of Values";
     cout << "\n\n";
     for ( list=0; list < 5; list++) {
      sum+=list[val];
      average = (sum/5);
      cout << "  " << list[val] << " ";
     }
     cout << "\n\n";
     cout << "\nThe total sum of the values is " << sum << ".";
     cout << "\nThe average of the values is " << average << ".";

     file << "\n\t ======== LIST OF VALUES  ========";
     file << "\n\n";
       for ( list=0; list < 5; list++) {
     file << "  " << list[val] << " ";

     }
     file << "\n";
     file << "\nThe total sum of the values is " << sum << ".";
     file << "\nThe average of the values is " << average << ".";
     file.close();
     cout << "\n\n";
     system("PAUSE");
}

No comments:

Post a Comment