C++ ???

Discussion in 'Mac Programming' started by DelisleBA.info, Sep 25, 2012.

  1. DelisleBA.info macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #1
    /*
    Hey internet,
    I am trying to write a program that adds a number to the end of a filename.
    */

    Code:
    
    #include <ostream>
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        int dayNum = 0;          // Day number.
        string filenameString; // Holder for sequential filename suffix.
        ostringstream convert; // Holder for int daynum to string conversion.
        
        string repFilename = "FILENAME"; //f Name of File.
         
        cout << endl;
        cout << "Day Number: "; // Get the day number.
        cin >> dayNum;
        
        cout << endl;
        
        convert << dayNum; // Assing dayNum to covert.
        
        filenameString = convert.str();  // Change dayNum from int to string.
        
        filenameString = "_DAY_" + filenameString;
        // Add "_day_" to dayNum suffix.
        
        filenameString = repFilename + filenameString + ".txt";
        // Add file name + dayNum suffix.
        
        cout <<"\n";
        cout << "File name is = " << filenameString; // TEST 1
        cout <<"\n";
        
        // TEST 2. should make filename = FILENAME_DAY_2.txt.
        
        dayNum++; // Assign dayNum + 1 to covert.
        
        convert << dayNum; // Assing dayNum to covert.
        
        filenameString = convert.str(); // Change dayNum from int to string.
        
        filenameString = "_day_" + filenameString;
        // Add "_day_" to dayNum suffix.
        
        filenameString = repFilename + filenameString + ".txt";
        // Add file name + dayNum suffix.
        
        cout <<"\n";
        cout << "NEW File name is = " << filenameString.c_str(); // TEST
        cout <<"\n";
        cout <<"\n";
    
        // but the file name is = FILENAME_day_12.txt
        
    } // END main.
    
    
    // the first pass works great the second pass does not
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I would suggest using breakpoints to inspect each variable along the way, but just looking at your code, I think I see your issue:

    You have a line of code that looks like this twice:

    Code:
    convert << dayNum;
    The first time, convert starts empty and is passed "1", so it becomes "1".

    The second time, convert starts "1" and is passed "2", so it becomes "12".

    That's my guess. I would suggest clearing (flushing?) convert after you're done using it the first time. (C++ isn't my strongest language...)
     
  3. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #3
    Look up documentation on: ostringstream

    You are not using it entirely correctly.
     
  4. DelisleBA.info thread starter macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #4
  5. Ap0ks macrumors 6502

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #5
    As ArtOfWarfare suggested you need to clear the contents of the convert object before you reuse it.

    A simple:
    Code:
    convert.str("");
    should suffice.
     
  6. DelisleBA.info thread starter macrumors member

    Joined:
    Apr 12, 2011
    Location:
    Athens, OH
    #6
    Thanks

    @Ap0ks,

    Worked like a charm. Thank you for helping.
     

Share This Page