Write a program in C++ to open a text file named \"CSC2134N.TXT\" for output, th
ID: 3821849 • Letter: W
Question
Write a program in C++ to open a text file named "CSC2134N.TXT" for output, then accept an integer from the console and a float value from the console (in separate input operations) and write the numbers to the file. An integer value of 0 should be used as a code value to cause the program to quit accepting numbers from the console and close the file.
Here is an example of how the input should be done:
Enter an integer (0 to quit): 34
Enter a float value: 2.37
Enter an integer (0 to quit): 127
Enter a float value: -42.7
Enter an integer (0 to quit): 0
(program ends)
Explanation / Answer
Please Check the program and let me know any doubts.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int A;
float B;
ofstream file2; //declare the file variable
file2.open("CSC2134N.TXT"); //open the file for output
if (!file2)
{
cout << "Unable to open CSC2134.TXT" << endl;
return 0;
}
while (A != 0)
{
cout << "Enter an integer OR 0 to Quit :";
cin >> A;
if (A != 0)
{
file2 << A << endl;
cout << "Enter the float value: ";
cin >> B;
file2 << B << endl;
}
else file2.close(); //close the file;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.