Statistics program

Discussion in 'C++' started by Nate, Mar 7, 2006.

  1. Nate

    Nate New Member

    Joined:
    Mar 7, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
    Last edited by a moderator: Mar 7, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice