Advertisement
Appleqt

File Processing

Oct 10th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void dataInput(){
  8. cout<<"\nFile opened for writing!\n";
  9. char file[] = "myNums.dat";
  10. ofstream outfile(file);
  11. double num;
  12. cout<<"Enter next number (-1 to quit): ";cin >> num;
  13. while(num!=-1){
  14. outfile<<num<<endl;
  15. cout<<"Enter next number (-1 to quit): ";cin >> num;
  16. }
  17.  
  18. outfile.close();
  19.  
  20. cout<<"\nFile has been closed for writing!\n";
  21.  
  22. }
  23.  
  24. double calcAverage(double total, int count){
  25. return total/count;
  26. }
  27.  
  28. double displayData() {
  29.  
  30. char file[] = "myNums.dat";
  31. ifstream infile(file);
  32. double total =0, num; int count=0;
  33.  
  34. cout<<"\nFile opened for reading!\n";
  35. while(infile>>num) {
  36. cout<<num<<endl;
  37. cout.width(2);
  38. total += num;
  39. count += 1;
  40. }
  41. infile.close();
  42.  
  43. cout<<"\nFile has been closed for reading!\n";
  44.  
  45. return calcAverage(total,count);
  46. }
  47.  
  48. int main(){
  49.  
  50. dataInput();
  51.  
  52. double average = displayData();
  53.  
  54. cout<<setprecision(2)<<fixed<<showpoint;
  55. cout<<"The average value read is " << average << endl;
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement