why does it keep coming up with errors

here the problem
Write a program named writeToFile.cpp which writes and formats the output. Ask the user for the filename and write to that file. Assume the user enters prices.txt as the file name. First do error checking to see if the file has opened successfully for writing. Then your program should write to the output file formatted as a table.

Books 39.95
CDs 3.22
Pens 1.08
Pencils .99

can you also let me know if I'm on the right track
here what I have so far:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main () {
ofstream fout;
string filename;
cout<<"please give me the filename to write to: ";
cin>> filename;
fout.open (filename, c_str());
if(fout.fail())
{
cout<<"file open fail!";
}
fout<< "Books 39.95"<<endl;
fout<<"CDs 3.22"<<endl;
fout<<"Pens 1.08"<<endl;
fout<<"Pencils .99" <<endl;
const double;
Books=39.95;
CDs=3.22;
Pens=1.08;
Pencils=.99;
fout<< setprecision(2)<<Books<<endl;
fout<< setprecision(2)<<CDs <<endl;
fout<< setprecision(2)<<Pens<<endl;
fout<< setprecision(2)<<Pencils<<endl;
fout.close();
Line 13: If the open failed, you output a message, but then continue on as if nothing adverse had happened.

Line 9: You're trying to cin to a string, but you haven't included the <string> header.

Line 11: It should be filename.c_str() not ,

Line 20: const double doesn't declare any variables.

Line 21-24,25-28: Books, CDs, Pens,Pencils are all undefined variables.

You're outputting the same information twice. Once at lines 16-19 and again at lines 25-28. This is not in the form of a table.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Topic archived. No new replies allowed.