I am making my first program and am having a little bit of trouble. I want to have the program create files that have a file name that is the same as an input, but an error always occurs when i try. Here is my code so far: Code: #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; int main () { string fname, lname, number, hits, n,; cout << "Enter first and last name "; cin >> fname; cin >> lname; cout << "Enter number "; cin >> number; cout << "Enter Hits "; cin >> hits; cout << lname; ofstream myfile (lname.txt_str()); if (myfile.is_open()) { myfile << "Name Number Hits.\n"; myfile << " "; myfile << ""<<lname<< ", " <<fname<< " " <<number<< " " <<hits<< "\n"; myfile << ""<<n<<".\n"; myfile.close(); } else cout << "Unable to open file"; cin >> n; return 0; }
There are couple of errors. 1. The error is at string fname, lname, number, hits, n,; replace with string fname, lname, number, hits, n; Note the last "," before the semicolon 2. Replace ofstream myfile (lname.txt_str()); With ofstream myfile (lname.c_str()); Thanks Shabbir