Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <cstdlib>
- #include <string>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- string getFileInfo();
- double calculateMean (string filename);
- int calculatorMin(string filename);
- //***********************************
- // Int main *
- // Where we tie in all the funtions *
- //***********************************
- int main ()
- {
- string filename = getFileInfo(); // getting file name
- double mean = calculateMean( filename); // avg
- int min = calculatorMin(filename); // min
- return 0;
- }
- //***************************************************
- //Function getFileInfo *
- //Function for getting the text file name from user *
- //***************************************************
- string getFileInfo() {
- string filename;
- ifstream inputFile;
- // asking user for name
- cout << "Please enter a file name to read from: ";
- cin >> filename;
- inputFile.open(filename);
- if (inputFile)
- {
- inputFile.close();
- }
- // if it doesnt open wrong ask for it again
- else
- {
- cout << "Error: Please enter a file name to read from: ";
- cin >> filename;
- }
- //if it opens then name is good and close file then return name
- //if it didn't open, ask for a new name.
- return filename;
- }
- //************************************************************
- //Function calculateMean *
- //funtion for the calculating of the mean of all the numbers *
- //************************************************************
- double calculateMean(string filename)
- {
- ifstream infile;
- float num=0;
- float total = 0.0;
- int count = 0;
- //varibles for finding median
- infile.open(filename);
- // While infile successfully extracted numbers from the stream
- while(infile >> num) {
- total += num;
- ++count;
- }
- // don't need the file anymore, close it
- infile.close();
- //output mean
- cout << "The mean was " << total/count << endl;
- // give the average
- return total/count;
- }
- //******************************************
- // Function calculatorMin *
- //funtion for calculating the minumum value*
- //******************************************
- int calculatorMin(string filename)
- {
- ifstream infile;
- double min = 32767; // since all the numbers are positive
- float num;
- // varibles
- infile.open(filename);
- while (infile >> num)
- {
- {
- if (num < min)
- min = num;
- } // algorithm for median
- }
- infile.close();
- cout << "The minumum number is " << min << endl;
- // out put minumum number
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement