// 16a2.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include #include #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] << " "; } }