-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise 2
120 lines (103 loc) · 3.05 KB
/
Exercise 2
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// -----------------------------------------------------------------------------
//
// C10 - Exercise 2
//
/* Write a program that creates a file of data in the form of the temperature Reading
type defined in 10.5. For testing, fill the file with at least 50 "temperature
readings". Call this program store_temps.cpp and the file it creates raw_temps.txt
*/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
struct Reading
{
int hour;
double temperature;
};
// -----------------------------------------------------------------------------
//check that the value being read in is a number
bool isNumber(string value)
{
size_t found = value.find_first_not_of("0123456789.");
if (found != string::npos)
{
cout << value << " is not a number and will be skipped." << endl;
return false;
}
return true;
}
// -----------------------------------------------------------------------------
//read in numbers from file (I'm being lazy as I cannot be bothered to type in 50 readings,
//so I'm reading in from a file that I copypasta'd)
vector<Reading> readIn(string filename)
{
vector<Reading> readings;
if (ifstream inFile{ filename })
{
string hour, temp;
while (!inFile.eof())
{
inFile >> hour >> temp;
if (isNumber(hour) && isNumber(temp))
readings.push_back(Reading{ stoi(hour), stod(temp) });
}
}
else
cout << "Error opening file " << filename << endl;
return readings;
}
// -----------------------------------------------------------------------------
//create a file using the data from readIn
void createFile(string filename)
{
if (ofstream outFile{ filename })
{
vector<Reading> readings = readIn("temps.txt");
for (Reading r : readings)
outFile << r.hour << " " << r.temperature << endl;
}
else
cout << "Error opening file " << filename << endl;
}
// -----------------------------------------------------------------------------
//create a file by asking for info
void createFile2(string filename)
{
if (ofstream outFile{ filename })
{
string hour, temp;
cout << "Please enter hours and temperatures in the following format: hour24 temperatureF (00 10.1)" << endl;
cout << "Enter ctrl + z to stop." << endl;
while (cin >> hour)
{
while (!isNumber(hour))
{
cout << "\nTry again: ";
cin >> hour;
}
cin >> temp;
while (!isNumber(temp))
{
cout << "\nTry again: ";
cin >> temp;
}
outFile << hour << " " << temp << endl;
}
}
else
cout << "Error opening file " << filename << endl;
}
// -----------------------------------------------------------------------------
int main()
{
string filename = "raw_temps.txt";
//createFile(filename);
//createFile2(filename);
char ch;
cin >> ch;
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------