Archived

This topic is now archived and is closed to further replies.

omegasyphon

setting an arry idex to a value problem

Recommended Posts

You display it with something like this, right?
  cout << ary[1];  

That should display a 2.

Post up your code.... maybe then I could help you out.


-Forcas


"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."



Share this post


Link to post
Share on other sites
It depends how the concatenation operator for the String object is defined. It also depends on if it is made to handle the type of values stored in your array. What kind of an array is it? Is it an array of characters, ints, foos?

-Forcas


"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."



Share this post


Link to post
Share on other sites
#include <sring>
using namespace std;
string str, cstr;
//
string arr[5]; // string array
char carr[5]; // character array
//
// concatenate strings
for(int n = 0; n < 5; ++n)
{
str += (arr[n] + " "); // you can append a string to a string
cstr += (carr[n] + " "); // you can append a string to a char to a string
}
//
cout << str << endl;
cout << cstr << endl;

There''s nothing wrong with the section of code you posted. Your error must be somewhere else.



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
He didn''t say that you could concatenate an int to a string.

You''ll have to use the sprintf funtion (in stdio.h .... I think)

concatenate this to your string: sprintf("%d", ary[1])

-Forcas


"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."



Share this post


Link to post
Share on other sites
quote:
Original post by omegasyphon
so if my ary was defined as in int it should still work?

Aha! You have confessed your sin unwittingly! Thou shalt pay for thine insufferance...

*Ahem* Sorry. You can''t concatenate two different data types in a strongly typed language! This isn''t Perl! (Long live Perl!) The C way:

char buf[SIZE]; // where size is as many digits your int will be + 1
sprintf("%d", buf, ary[j]); // or something like that; I don''t use C anymore

Alternative using strings:

// buffer must be allocated as above
char buf[SIZE];
str += itoa(ary[j], buf, 10); // base 10 conversion




I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
quote:
Original post by omegasyphon
well it works in my java program

Well C++ isn''t Java (and really isn''t that close either, other than syntax similarities). It works (as mentioned above) in Perl, PHP, Python, Ruby, Tcl, (insert scripting language here) because of the notion of "context". C++ (like C), OTOH, cannot afford such a languid luxury, being a "systems language."

Your point is?



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites