Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote