473,508 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing last written symbol from stream

Hello,

for(int i=0; i<100; ++i){
cout << i << ",";
}
// how can i remove last comma here?
......
cout << endl;

Jul 31 '06 #1
5 3950
Denis Petronenko wrote:
Hello,

for(int i=0; i<100; ++i){
cout << i << ",";
}
// how can i remove last comma here?
.....
cout << endl;
Use an if statement in the for-loop. If you're on the last iteration,
don't print one.

Cheers! --M

Jul 31 '06 #2

mlimber wrote:
Denis Petronenko wrote:
Hello,

for(int i=0; i<100; ++i){
cout << i << ",";
}
// how can i remove last comma here?
.....
cout << endl;

Use an if statement in the for-loop. If you're on the last iteration,
don't print one.

Cheers! --M
ok. lets see on following example.

i want to print elements of set divided by comma:

set<intmyset;
for(int i=0; i<10; ++i) myset.insert(random());
for_each(myset.begin(),myset.end(),printint());

i defined function object to do this job:

class printint
{
public:
printint(){}
~printint(){
// remove last comma here
cout << endl;
}

void operator()(int val){
cout << val << ",";
}
};

How can i remove or not write last comma here? function object don't
know how many elements stored in myset.

Thanks.

Jul 31 '06 #3
In article <11*********************@75g2000cwc.googlegroups.c om>,
"Denis Petronenko" <pe********@gmail.comwrote:
Hello,

for(int i=0; i<100; ++i){
cout << i << ",";
}
// how can i remove last comma here?
.....
cout << endl;
Don't put it in in the first place.

for ( int i = 0; i < 99; ++i )
cout << i << ',';
cout << 99 << endl;
Jul 31 '06 #4
Denis Petronenko wrote:
mlimber wrote:
Denis Petronenko wrote:
Hello,
>
for(int i=0; i<100; ++i){
cout << i << ",";
}
// how can i remove last comma here?
.....
cout << endl;
Use an if statement in the for-loop. If you're on the last iteration,
don't print one.

Cheers! --M

ok. lets see on following example.

i want to print elements of set divided by comma:

set<intmyset;
for(int i=0; i<10; ++i) myset.insert(random());
for_each(myset.begin(),myset.end(),printint());

i defined function object to do this job:

class printint
{
public:
printint(){}
~printint(){
// remove last comma here
cout << endl;
}

void operator()(int val){
cout << val << ",";
}
};

How can i remove or not write last comma here? function object don't
know how many elements stored in myset.

Thanks.
You either need to find some other way to do it, or you need to make
the functor stateful (e.g., have it keep its own count and pass the
upper limit in via the constructor).

Cheers! --M

Jul 31 '06 #5
Denis Petronenko wrote:
i defined function object to do this job:

class printint
{
public:
printint(){}
~printint(){
// remove last comma here
cout << endl;
}

void operator()(int val){
cout << val << ",";
}
};

How can i remove or not write last comma here? function object don't
know how many elements stored in myset.
But it can know when it's first called. So you can supress the first instead
of the last.

if (first)
first= false;
else
cout << ",";
cout << val;

--
Salu2
Jul 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2817
by: Falk Fassmann | last post by:
Hello people, I have some problem with Debugging in MS Visual C++. I want to generate a TCP/IP-Socket. The compiling-process is fine but when I debugg the project there's occuring some errors...
1
717
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
2081
by: Paul [Paradise Solutions] | last post by:
Hi All In a console app I have the following code: Sub Main() Dim p_CustomerLine As String Dim stream As IO.StreamReader = IO.File.OpenText("c:\test.txt") Do p_CustomerLine =...
3
5136
by: quat | last post by:
Hi, I have a char* string where I assigned a string literal with the British pound symbol, which is included in the extended ASCII, at least on my OS. However, when I cout the string, I get a...
35
11335
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
6
6083
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
6
1741
by: ineedahelp | last post by:
I have a table TEMPSYMBOL with a variable number of stock symbols in it. I need to use this table to filter a very large table called DAILYPRICE for the last instance of decent data 'MARKETPRICE'...
9
7245
by: Rajarshi | last post by:
Hi, I have some code that takes a string and obtains a compressed version using zlib.compress Does anybody know how I can remove the header portion of the compressed bytes, such that I only have...
9
2384
by: helraizer1 | last post by:
ChatPad The layout so far is pretty amateur but it'll improve. I spent an 1h30/2h or so in this tonight. The concept of ChatPad is basically a dynamic image acting as a shoutbox. <?php ...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.