setting an arry idex to a value problem

Started by
9 comments, last by omegasyphon 22 years, 5 months ago
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;
Advertisement
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."



-Forcaswriteln("Does this actually work?");
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<
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."



-Forcaswriteln("Does this actually work?");
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.
#include &ltsring>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!
so if my ary was defined as in int it should still work?
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."



-Forcaswriteln("Does this actually work?");
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!
well it works in my java program

This topic is closed to new replies.

Advertisement