Why does it say that the ofstream : no such file or directory. What do I need to
ID: 3683137 • Letter: W
Question
Why does it say that the ofstream : no such file or directory. What do I need to add?
// This program writes user input to a file.
#include <iostream>
#include <ofstream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream outputFile; // File stream object
int numberOfDays; // Number of days of sales
double sales; // Sales amount for a day
// Get the number of days.
cout << "For how many days do you have sales? ";
cin >> numberOfDays;
// Open a file named Sales.txt.
outputFile.open("sales.txt");
// Get the sales for each day and write it
// to the file.
// loop to enter the value.
for (int count = 1; count <= numberOfDays; count++)
{
// Get the sales for a day.
cout << "Enter the sales for day "
<< count << ": ";
cin >> sales;
// Write the sales to the file.
outputFile << sales << endl; // make sure to add endl
// cooutput file inside the loop
}
// Close the file.
outputFile.close();
cout << "Data written to Sales.txt ";
ifstream inputFile; // be able to read
int number; // read numbers
int total = 0;
inputFile.open("sale.txt");
if (inputFile)
{
// Read the numbers from the file and
// display them.
while (inputFile >> number) // while loop can read the number
{
cout << number << endl;
total += number;
}
cout << "total is" <<total;
}
else
cout << "Error file not found.";
inputFile.close();
outputFile.open ("report.txt");
cout <<total;
system ("pause");
return 0;
}
Explanation / Answer
There is a slight change in the program that has been reflected in the comments beside them. With this change in the code we do not have to edit anything else in the program.
// This program writes user input to a file.
#include <iostream>
// #include <ofstream> //THERE EXISTS NO SUCH FILE IN THE DIRECTORY WITH THE NAME oftream.h , SO IT CAN BE REMOVED
#include <fstream> //FSTREAM CONTAINS BOTH ifstream AND ofstream
#include <string.h>
using namespace std;
int main()
{
ofstream outputFile; // File stream object
int numberOfDays; // Number of days of sales
double sales; // Sales amount for a day
// Get the number of days.
cout << "For how many days do you have sales? ";
cin >> numberOfDays;
// Open a file named Sales.txt.
outputFile.open("sales.txt");
// Get the sales for each day and write it
// to the file.
// loop to enter the value.
for (int count = 1; count <= numberOfDays; count++)
{
// Get the sales for a day.
cout << "Enter the sales for day "
<< count << ": ";
cin >> sales;
// Write the sales to the file.
outputFile << sales << endl; // make sure to add endl
// cooutput file inside the loop
}
// Close the file.
outputFile.close();
cout << "Data written to Sales.txt ";
ifstream inputFile; // be able to read
int number; // read numbers
int total = 0;
inputFile.open("sale.txt");
if (inputFile)
{
// Read the numbers from the file and
// display them.
while (inputFile >> number) // while loop can read the number
{
cout << number << endl;
total += number;
}
cout << "total is" <<total;
}
else
cout << "Error file not found.";
inputFile.close();
outputFile.open ("report.txt");
cout <<total;
system ("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.