Question:
How to append an array of integers to a string (C++)?
jam4u06
2012-01-27 07:31:24 UTC
I am trying to add a whole array of numbers to a string. Here is the pseudocode:
for(int y = 0; y != length + 1; y++)
{
add decimal[y] to the end of NumberString
}

And i just can't seem to get the part inside the loop.
NumberString is the string i am trying to add the array of decimal to.
Four answers:
cja
2012-01-27 07:55:27 UTC
I'm not exactly sure what you're trying to do, but the example below should help. Note that looping while "y != length + 1" seems likely to index outside the bounds of your array.Typically you'd loop: for (int y = 0; y < length; y++) { . . .



#include

#include

#include



using namespace std;



int a[] = { 0, 1, 2, 99 };

string intArray2Str(int *, size_t);



int main(int argc, char *argv[]) {

    size_t aLen = sizeof(a) / sizeof(a[0]);



    cout << intArray2Str(a, aLen) << endl;



    return 0;

}



string intArray2Str(int *a, size_t n) {

    stringstream ss;



    for (size_t i = 0; i < n; i++) {

        ss << a[i];

    }

    return ss.str();

}



#if 0



Program output:



01299



#endif
?
2017-01-11 23:52:20 UTC
individual myArray[entries]; isn't fairly real the two. you may no longer declare an array with a length that's no longer nicely-known at carry at the same time time. you should use new to create the array: individual * myArray = new individual[entries]; you have a 2d situation: you attempt to create the array interior the for loop. yet you do no longer prefer to create the array each and every time in the process the loop. you prefer to create it as quickly as, real before you initiate the loop. The variable individual documents isn't helping you. you place each and every of the suggestion into documents, yet you do no longer keep it everywhere. What you particularly need to do is examine the suggestion into between the guy entries in myArray. even nevertheless myArray is asserted as a pointer, you should use it as an array. that's what you particularly need: cin>>myArray[i].phoneNumber; which will examine the telephone # into the i'th element of the array, so each and every time in the process the loop, you will keep the telephone form in a sparkling 'slot'. >> i could like for somebody to easily do the comprehensive component for me so i'm able to study.... you may no longer study that way. Programming is a study by doing interest.
anonymous
2012-01-27 19:29:10 UTC
Try this:



#include

#include

#include



. . . . .



char buffer[255];

int len;

buffer[0] = '\0';

string NumberString;

. . . . .



for(int y = 0; y != length + 1; y++)

{

len = strlen (buffer);

sprintf (&buffer[len], "%d", decimal[y]);

}

NumberString = buffer;



. . . ..



cout << NumberString;
Ella
2012-01-27 07:33:26 UTC
NumberString += " ";

char buf[16];

NumberString += itoa(i,buf,10);



where i is the number to append


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...