Write a C++ program that prompts a user to enter three numbers, and writes the n
ID: 641408 • Letter: W
Question
Write a C++ program that prompts a user to enter three numbers, and writes the numbers to a file called "numbers.txt" Close the file before exiting the program. This is how my file currently looks but I cant get it to work.
float n1,n2,n3;
ofstream ofs, open("numbers.txt");
cout << "What is your favorite number?" << endl;
ofs >> n1;
cout << "What is your second favorite number?" << endl;
ofs >> n2;
cout << "What is your third favorite number?" << endl;
ofs >> n3;
ofs.close();
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main(){
float n1,n2,n3;
ofstream ofs;
ofs.open("numbers.txt");
cout << "What is your favorite number?" << endl;
cin >> n1;
ofs << n1 << endl;
cout << "What is your second favorite number?" << endl;
cin >> n2;
ofs << n2 << endl;
cout << "What is your third favorite number?" << endl;
cin >> n3;
ofs << n3 << endl;
ofs.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.