TStringList, Integer with leading zero's

Oct 27, 2015 at 7:54am
closed account (Shp21hU5)
Hi Guys or ladies can you help please
I’m trying to write an integer value to a TStringList with leading zero’s.
I’ve tried using setw() & setiosflags like I would normally if I write to a stream but I keep getting a message saying “Could not find a match for ios_manip” or something similar.
Can you help please, is there a way round it ?
Oct 27, 2015 at 9:39am
closed account (48T7M4Gy)
#include <iomanip>

http://www.cplusplus.com/reference/iomanip/setw/
Oct 27, 2015 at 1:47pm
closed account (Shp21hU5)
All ready got that included, but it still says you can't use it within a TStringList because it's not supported.
Oct 27, 2015 at 2:05pm
Hi,

Can you show the relevant code? Ideally code that we can compile ourselves.

And any error messages exactly with the right line numbers?


It sounds like you are trying to use << on a class that doesn't have a ostream<< operator.
Oct 28, 2015 at 10:07am
closed account (Shp21hU5)
Hi TheIdeasMan

Thank you for your response, you are right I am trying to use the << in code I don't know I way round it.

I enclose some code so you can see what I'm trying to do.

thank you for taking the time to apply in the first place.

//---------------------------------------------------------------------------

#include <vcl.h>


#include <io.h>
#include <iomanip.h>
#include <math.h>

#include <string>
#include <iostream>
#include <sstream>

#pragma hdrstop


#include "Test_Prog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TTestProg *TestProg;
//---------------------------------------------------------------------------
__fastcall TTestProg::TTestProg(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TestProg::Write_Code(TObject *Sender)
{

float Precision;

int Val_1 = 300; //Rem:: This value is normally any value entered by user !


TStringList* MyList;

wstringstream inout;



if(Precision_Opt1->Checked == true)
{
Precision = 3;
}
else
{
Precision = 4;
}



/* ------ New Way ! ----- */
MyList = new TStringList();

MyList->Clear();


/* Note:
All I want it do is to input the 'Val_1' entered by the user with leading
zero's into MyList.
e.g.
000300
002800
000023
*/

/*Rem:: This code doen't work and displays the Message::
Can not convert wostream to AnsiString !. */
MyList->Append(inout << setw(Precision+1) << setiosflags(ios::right) << setfill('0') << Val_1);


/*Rem:: This code doen't work and displays the Message::
[C++ Error] TestProg.cpp(1253): E2094 'operator <<' not implemented in type '_Ios_Manip_1<int>' for arguments of type '_Ios_Setf_Manip'*/
MyList->Append( setw(Precision+1) << setiosflags(ios::right) << setfill('0') << Val_1);

}
//--------------------------------------------------------------------------
Oct 28, 2015 at 10:58am
Hi,

Have a go with std::stringstream I haven't used for something like this, but I am guessing one can use the same formatting arguments that std::cout has, to make a string. Then it might work with your Append function, if it accepts std::string as an argument.

Some other things I am guessing about, I have no idea about how mad they be lol :

Overload the operator>> for your MyList class.

Good Luck !!
Oct 31, 2015 at 2:16pm
closed account (Shp21hU5)
Hi TheIdeasMan

Thank you so much for you help I've managed to find a way round the problem and it works fine, It may not be the best way but it works. here my code.

wstringstream inout;

inout<< setw(Precision+1)<< setiosflags(ios::right) << setfill('0')<< iD_Value;

wchar_t p[9];

memset(p,0,sizeof(p));

inout.getline(p,Precision+2);

char *str = new char[iPrecision + 2];

memset( str, 0, iMaxPrecision + 2);

wcstombs(str, p, iMaxPrecision);

int Size = strlen(str);
Size++;
char *Buffer = new char[Size];
StrCopy(Buffer,str);

MyList->Add(Buffer);


Once again Thank You.
Topic archived. No new replies allowed.