Write a program to open a text file named \"CSC2134N.TXT\" for output, then acce
ID: 3815901 • Letter: W
Question
Write a program 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 C++ format, preferably codeblocks or notepad
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outputFile;
outputFile.open ("CSC2134N.TXT");
int n;
float f;
cout<<"Enter an integer (0 to quit): ";
cin >> n;
while(n!=0) {
cout<<"Enter a float value: ";
cin >> f;
outputFile << n<<" "<<f<<endl;
cout<<"Enter an integer (0 to quit): ";
cin >> n;
}
cout<<"File has been generated"<<endl;
outputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter an integer (0 to quit): 34
Enter a float value: 2.34
Enter an integer (0 to quit): 127
Enter a float value: -42.7
Enter an integer (0 to quit): 0
File has been generated
CSC2134N.TXT
34 2.34
127 -42.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.