Bitcoin Forum

Economy => Services => Topic started by: CatsLikeToStretch on April 01, 2012, 03:20:10 PM



Title: [Completed Transaction]Newbie C++ help [2BTC] Bounty
Post by: CatsLikeToStretch on April 01, 2012, 03:20:10 PM
I am trying to teach myself C++ and I am stuck on something.  I am trying to write a program that writes a series of image files (in .pbm format).  I want the files to be named based on the value of an integer variable.  For example, if the variable contains the value 1045 then I want to output data to a file called 1045.pbm.  I will pay you 1/2 of the bounty if you supply code that works and I'll pay the other half for explaining to me, in terms that a complete beginner like myself, what the purpose of the various bits are code are.  Thanks in advance.  

If you don't believe I will actually pay - you can check https://bitcointalk.org/index.php?topic=73989.0 on the beginner board where I have already paid out for help.  Thanks in advance.


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: Matoking on April 01, 2012, 04:16:35 PM
What IDE are you using at the moment? (I use Qt Creator, for an example, some others I can recall from the top of my head are Microsoft Visual Studio and Code::Blocks)

The content of the .pbm files don't have to be anything specific, if I'm correct?


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: CatsLikeToStretch on April 01, 2012, 04:20:53 PM
I am using geany.  I am running ubuntu 10.04. 

Quote
The content of the .pbm files don't have to be anything specific, if I'm correct?

I am writing data to each of the files.  I am trying to generate a series of images, and the name of the image files should be based on the value of an integer variable in the code.


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: yogi on April 01, 2012, 04:30:33 PM
#include <sstream>

std::string GenFileName(int i)
{
  std::stringstream out;
  out << i << ".pbm";
  return out.str();
}


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: Matoking on April 01, 2012, 05:02:46 PM
Code:
#include <iostream>
#include <fstream> // Required, so we can use ofstream
#include <sstream> // Required, so we can use std::stringstream

using namespace std;

int main()
{
    // For statement for looping 10 times
    for(int i=1; i<=10; i++)
    {
        // Tell the user what file number we are working with
        cout << "File number: " << i << endl;

        // Create  a ofstream, which can be used to write into files
        ofstream file;

        // Create a std::stringstream, to which we can input information
        std::stringstream stream;

        // Input the filenumber and the file type to the stream
        // For example, if the value of i was 5, the content of stream would be "5.pbm"
        stream << i << ".pbm";

        // This looks ugly, the stream is converted into std::string, which is converted into char*, which is needed for this function,
        // because it won't accept std::stringstream or std::string types
        file.open(stream.str().data());

        // Warn the user if the file couldn't be opened
        if (file.is_open() == false)
            cout << "Couldn't open the file\n";

        // Define the file is a PBM (portable bitmap) with a magic number
        file << "P1\n";

        // Define the width and height of the image ( width is 6 and height is 4 )
        file << "6 4\n";

        // Let's input the actual data ( \n is used to mark new lines )
        file << "1 0 1 0 1 0\n";
        file << "1 1 1 0 1 0\n";
        file << "1 1 1 0 1 0\n";
        file << "1 0 1 0 1 0";

        // Save and close the file
        file.close();
    }

    // Informs the program has executed successfully and will now close
    return 0;
}

This program should create ten .pbm files.

I'll have to admit I'm not very familiar with std, so the file.open(stream.str().data()) portion could be probably done in a better way.


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: CatsLikeToStretch on April 01, 2012, 06:48:36 PM
Quote
file.open(stream.str().data());

Thats the bit I couldn't get right before.  Thanks for your help and explanation.  2.0001 BTC were just sent to

1M8Xa6crWwrymzUayCML1MJGqjF4JdGHHx  They should show up in the next bock.

Thanks for your help.


Title: Re: Newbie C++ help [2BTC] Bounty
Post by: Matoking on April 01, 2012, 06:52:31 PM
Thanks very much. :)