Thread: Need a new way to replace a constant

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Need a new way to replace a constant

    This is a program I wrote to replace a symbol with a date in specialized htm files. It works, but every time I run it I have to run the replace all command to replace the date. Is there a way to make it so I can have the date be a variable I enter when I start the program, so I don't have to keep compiling it before I run it each time. The date in this case is 102601. Any help you can offer would be greatly appreciated. Thank you


    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>

    //The following are the functions for the different htm's


    /////////////////////////////////////////////////////
    //cw3amp date replace function
    /////////////////////////////////////////////////////
    void cw3amp(ifstream& in_stream, ofstream& out_stream)
    {
    char next;

    in_stream.get(next);
    while (! in_stream.eof())
    {
    if (next == '@')
    out_stream << "102601";
    else
    out_stream << next;

    in_stream.get(next);
    }
    }

    //////////////////////////////////////////////////////
    int main()
    {

    //////////////////////////////////////////////////////
    // This is for cw3amp
    /////////////////////////////////////////////////////
    ifstream fin3amp;
    ofstream fout3amp;

    cout << "Begin editing cw3amp070901.\n";

    fin3amp.open("cw3amp070901.htm");
    if (fin3amp.fail())
    {
    cout << "cw3amp070901 opening failed.\n";
    exit(1);
    }

    fout3amp.open("cw3amp.htm");
    if (fout3amp.fail())
    {
    cout << "cw3amp opening failed.\n";
    exit(1);
    }

    cw3amp(fin3amp, fout3amp);

    fin3amp.close();
    fout3amp.close();

    cout << "End of editing cw3amp.\n";

    ///////////////////////

    return 0;
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could do this -

    void cw3amp(ifstream& in_stream, ofstream& out_stream)
    {
    char next;
    string date;
    cout << "Enter Date: ";
    cin >> date;

    in_stream.get(next);
    while (! in_stream.eof())
    {
    if (next == '@')
    out_stream << date;
    else
    out_stream << next;

    in_stream.get(next);
    }
    }

    You'll have to include <string>.

    If you need the date based on the current date, you could automate the process further by using some of the functions in the <ctime> header.
    zen

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    6
    Maybe I'm doing this wrong, but with the information from above I changed it to the following, and I keep getting the errors that follow. I haven't been able to get the string to work.

    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>

    //The following are the functions for the different htm's


    /////////////////////////////////////////////////////
    //cw3amp date replace function
    /////////////////////////////////////////////////////

    void cw3amp(ifstream& in_stream, ofstream& out_stream)
    {
    char next;
    string date;
    cout << "Enter Date: ";
    cin >> date;

    in_stream.get(next);
    while (! in_stream.eof())
    {
    if (next == '@')
    out_stream << date;
    else
    out_stream << next;

    in_stream.get(next);
    }
    }

    //////////////////////////////////////////////////////
    //////////////////////////////////////////////////////
    int main()
    {
    //////////////////////////////////////////////////////
    // This is for cw3amp
    /////////////////////////////////////////////////////
    ifstream fin3amp;
    ofstream fout3amp;

    cout << "Begin editing cw3amp070901.\n";

    fin3amp.open("cw3amp070901.htm");
    if (fin3amp.fail())
    {
    cout << "cw3amp070901 opening failed.\n";
    exit(1);
    }

    fout3amp.open("cw3amp.htm");
    if (fout3amp.fail())
    {
    cout << "cw3amp opening failed.\n";
    exit(1);
    }

    cw3amp(fin3amp, fout3amp);

    fin3amp.close();
    fout3amp.close();

    cout << "End of editing cw3amp.\n";



    return 0;
    }


    ---------------
    Errors:

    C:\My Documents\C++\Programs\htm date change\date change test.cpp(28) : error C2065: 'string' : undeclared identifier
    C:\My Documents\C++\Programs\htm date change\date change test.cpp(28) : error C2146: syntax error : missing ';' before identifier 'date'
    C:\My Documents\C++\Programs\htm date change\date change test.cpp(28) : error C2065: 'date' : undeclared identifier

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The include file is <string> not <string.h>. Your include files want to be something like -

    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <string>

    using namespace std;
    zen

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    42
    Can't you do this


    Code:
    
    In the h file, you write: 
    
                            
    struct TestOne 
                             
    { 
                             
    static const double MY_CONSTANT; 
                            
    }; 
    
    
    and in the cpp file, you write 
                             
    
    const double TestOne::MY_CONSTANT =12356.2 ;

    Marky_Mark

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    6
    I had my headers wrong, thank you for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM