-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment.old.cpp
96 lines (80 loc) · 2.83 KB
/
assignment.old.cpp
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
/* CS-210 - Assignment 5-2: File I/O
* Operations and temperature unit conversion.
* By Alexander Ahmann <[email protected]>
*
* Written & tested in Linux and compiled with g++ like so:
* aleksey@homepc:~/ g++ --Wall assignment.cpp
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
double fahrenheitToCelsius(double);
int writeOutput(string, int, int);
int main() {
ifstream entries;
string city;
double tempF;
// Try to open the file for conversion inputs
entries.open("FahrenheitTemperature.txt");
if (!entries.is_open()) {
cerr << "Could not open file "
<< "FahrenheitTemperature.txt" << endl;
return -1;
}
// Enumerate input file for conversions
do {
entries >> city;
entries >> tempF;
double conversion = fahrenheitToCelsius(tempF);
writeOutput(city, tempF, conversion);
entries.ignore();
entries.clear();
getline(entries, city);
} while (entries);
entries.close();
return 0;
}
/* Function to convert a temperature inputed as
* fahrenheit to celsius.
* @param fahrenheit - the fahrenheit input
* @returns the fahrenheit converted to celsius
*/
double fahrenheitToCelsius(double fahrenheit) {
return (5.0 / 9.0) * (fahrenheit - 32.0);
}
/* Function to write the results of conversion
* to an output file
* @param city - the conversion output to write
* @param fahrenheit - integer of converted unit
* @param celsius - integer of measure pre-conversion
* @returns an code where "0" indicates successful
* execution and "-1" indicates failure.
*/
int writeOutput(string city, int fahrenheit, int celsius) {
ofstream newEntries;
newEntries.open("CelsiusTemperature.txt", ios::app); // See [3]
if (!newEntries.is_open()) {
cerr << "Could not open file "
<< "CelsiusTemperature.txt" << endl;
return -1;
}
cout << setprecision(4) << city << " " << fahrenheit
<< " " << celsius << endl; // See [2]
newEntries << setprecision(4) << city << " "
<< fahrenheit << " " << celsius << endl; // See [2]
newEntries.close();
return 0;
}
/* Endnotes:
* [1] I referenced this tutorial when writing this code in general: "How to
* Read From a File in C++"; Retrieved on May. 31, 2023 from:
* https://www.udacity.com/blog/2021/05/how-to-read-from-a-file-in-cpp.html
* [2] I referenced this tutorial when figuring out how to round numbers
* (Retrieved on May. 31, 2023 from):
* https://www.programiz.com/cpp-programming/float-double
* [3] I referenced the following tutorial when learning to append to a text
* file rather than overwriting it (Retrieved on Jun. 1, 2023 from):
* https://www.geeksforgeeks.org/cpp-program-to-append-a-string-in-an-existing-file/
*/