Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- void dataInput(){
- cout<<"\nFile opened for writing!\n";
- char file[] = "myNums.dat";
- ofstream outfile(file);
- double num;
- cout<<"Enter next number (-1 to quit): ";cin >> num;
- while(num!=-1){
- outfile<<num<<endl;
- cout<<"Enter next number (-1 to quit): ";cin >> num;
- }
- outfile.close();
- cout<<"\nFile has been closed for writing!\n";
- }
- double calcAverage(double total, int count){
- return total/count;
- }
- double displayData() {
- char file[] = "myNums.dat";
- ifstream infile(file);
- double total =0, num; int count=0;
- cout<<"\nFile opened for reading!\n";
- while(infile>>num) {
- cout<<num<<endl;
- cout.width(2);
- total += num;
- count += 1;
- }
- infile.close();
- cout<<"\nFile has been closed for reading!\n";
- return calcAverage(total,count);
- }
- int main(){
- dataInput();
- double average = displayData();
- cout<<setprecision(2)<<fixed<<showpoint;
- cout<<"The average value read is " << average << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement