Archived

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

setting an arry idex to a value problem

Recommended Posts

omegasyphon    100
if i have an arry like so ary[4]=0; why is it that when i do this and display it i get a 0 ary[1]=2;

Share on other sites
Forcas    181
You display it with something like this, right?
  cout << ary[1];

That should display a 2.

-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 on other sites
omegasyphon    100
hmm i think i know the problem

would this cause a display problem

String str ="";
for (int x=0;x<4;x++)
{
str+=ary[x] +" ";
}

cout<

Share on other sites
Forcas    181
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 on other sites
merlin9x9    174
It seems that a lot of people around here don''t understand strings; they expect them to work in C/C++ as they do in, say, Visual Basic.

Share on other sites
Oluseyi    2103
#include <sring>using namespace std;string str, cstr;  //string arr[5]; // string arraychar carr[5];  // character array  //// concatenate stringsfor(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 on other sites
omegasyphon    100
so if my ary was defined as in int it should still work?

Share on other sites
Forcas    181
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 on other sites
Oluseyi    2103
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 + 1sprintf("%d", buf, ary[j]); // or something like that; I don''t use C anymore

Alternative using strings:
// buffer must be allocated as abovechar buf[SIZE];str += itoa(ary[j], buf, 10); // base 10 conversion

I wanna work for Microsoft!

Share on other sites
omegasyphon    100
well it works in my java program

Share on other sites
Oluseyi    2103
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."