Statistics program

Nate's Avatar
Newbie Member
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: CPP
#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";
  else cout << "Unable to open file";
  cin >> n;
  return 0;

Last edited by shabbir; 7Mar2006 at 14:15.. Reason: Edited to add syntax highlighting
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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());
ofstream myfile (lname.c_str());