aboutsummaryrefslogtreecommitdiff
path: root/16a2/16a2.cpp
blob: 8131561f9df3e986156c1edd7823d3f2d6e1dbec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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] << " ";
    }
}