diff options
Diffstat (limited to '16a2/16a2.cpp')
| -rw-r--r-- | 16a2/16a2.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/16a2/16a2.cpp b/16a2/16a2.cpp new file mode 100644 index 0000000..8131561 --- /dev/null +++ b/16a2/16a2.cpp @@ -0,0 +1,83 @@ +// 16a2.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include <iostream> +#include <string> +#include <algorithm> +#include "16a2.h" + +using namespace std; + +const int AMOUNT = 10; + +void getFileName(string& fileName); + +int readFile(FILE* filep, int nums[AMOUNT]); + +void output(int nums[AMOUNT]); + +int main() +{ + string fileName = ""; + FILE* fp; + int nums[AMOUNT]; + + getFileName(fileName); + char fn[5000]; + strcpy_s(fn, fileName.c_str()); + int error = fopen_s(&fp, fn, "r"); + if (error) + { + cout << "Error opening file. quitting..."; + return 1; + } + readFile(fp, nums); + fclose(fp); + + output(nums); + +} + +void getFileName(string& fileName) +{ + cout << "Input input the file name to read from: "; + getline(cin, fileName); + + while (fileName._Equal("")) + { + cout << "Unknown file."; + cout << "Input input the file name to read from: "; + getline(cin, fileName); + } +} + +int readFile(FILE* filep, int nums[AMOUNT]) +{ + int data = 0; + for (int i = 0; i < AMOUNT; i++) + { + if (fscanf_s(filep, "%i ", &data)) + { + nums[i] = data; + } + else + { + cout << "Error reading data, please check file and try again."; + return 1; + } + } + return 0; +} + +void output(int nums[AMOUNT]) +{ + sort(nums, nums + AMOUNT); + cout << "The smallest value is: " << nums[0] << endl; + cout << "The largest value is: " << nums[AMOUNT - 1] << endl; + + cout << "The whole list:" << endl; + for (int i = 0; i < AMOUNT; i++) + { + cout << nums[i] << " "; + } +}
\ No newline at end of file |