Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Statistics program (http://www.go4expert.com/forums/statistics-program-t615/)

Nate 7Mar2006 12:32

Statistics program
 
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";
    myfile.close();
  }
 
  else cout << "Unable to open file";
  cin >> n;
 
  return 0;
}


shabbir 7Mar2006 14:56

Re: Statistics program
 
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


All times are GMT +5.5. The time now is 01:59.