-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA13writetofile.cpp
63 lines (49 loc) · 1.49 KB
/
A13writetofile.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
// Write to File
// By Emily Dayanghirang
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile("output.txt");
int userInput;
char repeat;
do
{
// Repeatedly checks whether the user entered an invalid input
do
{
cout << "\nGive an integer input in the range 20 to 40 inclusive: ";
cin >> userInput;
// Input validation
if (cin.fail())
{
cout << "\nERROR: Please input an integer.\n";
cin.clear();
cin.ignore(100, '\n');
}
if(userInput < 20 || userInput > 40)
{
cout << "\nInput must be an integer in the range 20 to 40 inclusive.\n";
}
} while (userInput < 20 || userInput > 40);
// Check if the file is successfully opened
if(outfile.is_open())
{
// Write user's input to file
outfile << userInput << endl;
cout << "\nYour input " << userInput << " is now written on file.\n";
// Prompt user to continue or exit
cout << "\nPress 'Y' or 'y' to give another integer input or 'N' or 'n' to exit: ";
cin >> repeat;
}
else
{
cerr << "Error opening the file";
cerr << "Error code: " << strerror(errno);
exit(1);
}
} while(repeat != 'N' && repeat != 'n');
outfile.close();
return 0;
}