473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple string management

hello, i would like to increment a string in a for loop. The desired
output would look like:

team0
team1
team2

and so on. I use the code

for (int i = 0; i < numTeams; i++) {
sName = "team"+i;
a[i].SetName(sName);
}

and get the output:

team
eam
am

and so on. What can i do to get the desired effect in the most simple
way possible? thank you

Jul 23 '05 #1
4 1480
gu******@gmail.com wrote:
hello, i would like to increment a string in a for loop. The desired
output would look like:

team0
team1
team2

and so on. I use the code

for (int i = 0; i < numTeams; i++) {
sName = "team"+i;
a[i].SetName(sName);
}

and get the output:

team
eam
am


Amusing. What I believe you are doing is getting a memory address from
"team" and then you are adding a unit of i byte(s) to that memory
address with the +i. In C++ you can't use the + operator for string
manipulation. From the best of my knowledge you will need to convert the
integer i to an ascii then append it to the string using something like
strcat which can be found at:

http://www.cplusplus.com/ref/cstring/

Of course if you use google you will find all this information very easily.
Jul 23 '05 #2
> and so on. What can i do to get the desired effect in the most simple
way possible? thank you


Use string stream.

ben
Jul 23 '05 #3
gu******@gmail.com wrote:
hello, i would like to increment a string in a for loop. The desired
output would look like:

team0
team1
team2

and so on. I use the code

for (int i = 0; i < numTeams; i++) {
sName = "team"+i;
a[i].SetName(sName);
}

and get the output:

team
eam
am

and so on. What can i do to get the desired effect in the most simple
way possible? thank you


The simplest possible way, assuming sName is a std::string, is:

sName = "team";
sName += "0123"[i];

But this has limited use and of course you have to be careful not to
overrun the string literal. The best solution is to use
std::stringstream as was already suggested.

Jul 23 '05 #4
gu******@gmail.com wrote:
hello, i would like to increment a string
I think you should ask yourself what you actually mean here.
in a for loop. The desired
output would look like:

team0
team1
team2

and so on.


OK, so do you want to:

a) Increment the ASCII code of the last character of the string, starting at '0'.

b) Increment the integer value of the last bit of the string, assuming it to be of base x, and starting at 0.

c) Append to a string, a string representation of a number, which is of base x, starting at 0, which you are able to increment, in a loop.

I would guess that you mean option c, in which case the implementation should be a bit more obvious, when expressed in that way.

Also, do you have any bounds on these numbers? What happens at these bounds?

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #5

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

Similar topics

3
by: farialima | last post by:
Hello, My wife is taking care of a non-profit, and she asked me to help her set up an extranet for the the members. I've promissed I'd be done before the end of the year... now I have to deliver...
3
by: Jeff Uchtman | last post by:
Has anybody found a simple, no zone, banner ad management app? I just want click through and a start stop date function. I have see everything from soup to nuts on ad management form $10.00 to...
3
by: Scott Brady Drummonds | last post by:
Hi, all, I've a fairly small piece of code that is causing me problems. I'm using std::string and am building a string of several dozen characters using several of std::string's functions: a...
2
by: Abel Chan | last post by:
Hi there, I just got an assignment to work on server maintenance. It is a weekly task and we have about 7 production servers running Win2K server. The tasks include but not limited to 1)...
9
by: Andrew | last post by:
Hi, I implemented a simple WMI Provider in C#. It is a service which expose 10 instances of a simple WMI Class. The WMI class pnly expose 4 public properties (Value,Min,Max,StdValue) which...
4
by: martinpe | last post by:
Does anybody know a simple admin panel to use to manage little applications? I need only an user/admin basic structure. Thanks.
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
2
by: nicholas08 | last post by:
I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item. Attaching the menu so it's clear...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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...

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.