Write a program to open the text file named \"CSC2134N.TXT\" that you created in
ID: 3815903 • Letter: W
Question
Write a program to open the text file named "CSC2134N.TXT" that you created in prior assignment for input, then read all the numbers from the file and display them on the screen. You must read an integer value from the file, and then a float value. You may not simply read the lines as character data. You may assume that the integer and float values exist as pairs in the file.
Prior Assignment
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)
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream inputFile;
inputFile.open("CSC2134N.TXT");
int n;
float f;
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> n>>f;
cout<<n<<" "<<f<<endl;;
}
}
inputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
34 2.37
127 -42.7
CSC2134N.TXT
34 2.37
127 -42.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.