tags:

views:

145

answers:

4

Hello all

I am studying for a C++ test, I have not been able to solve this:

I have data in a int* and I want to print it out to a char*

I thought I could do it the following way:

// pOutputString is of type char*
// pIntegers is of type int*

int pos = 0;
for (int i = 0; i< SIZE; i++)
{
  pOutputString[pos] = char(pIntegers[i]); 
  pos++;
}

if I iterate over pOutputString with iostream, I should get the tring representation (so 11 will turn into "11").

As part of this homework, I'm not allowed to use any third-party libs. If I want something, I must do it myself.

Thanks.

Edit: You can see the entire code in IDEone: http://ideone.com/nKXpg

+1  A: 

If you mean that you have an array of integer and that you want to print the character representation of these integers (assuming it is possible), you could try something like :

// int in[SIZE] is given
char out[SIZE];
for(int i = 0; i < SIZE; i++)
{
  out[i] = static_cast<char>(in[i]);
}
std::cout << out;

By the way it is not a conversion between int* and char* but between int and char.

The following code does exactly that:

#include <iostream>
int main()
{
  int in[] = {50,55,35};
  char out[3];
  for(int i = 0; i < 3; ++i)
  {
    out[i] = static_cast<char>(in[i]);
  }
  std::cout << out << std::endl;
  return 0;
}
Cedric H.
+3  A: 

Well, the standard library is not third-party, so use that.

#include <sstream>
#include <cstring>
using namespace std;

ostringstream s;
for (int i = 0; i< SIZE; i++) {
    s << ( i? ", " : "" ) << pIntegers[i];
}
strcpy( pOutputString, s.str().c_str() );

There was a header <strstream> and class ostrsream that could do this without strcpy, but it's been deprecated as it was too easy to allow overflow.

(I'm making an assumption here about the input and output data formats, but iostreams probably has a way to format as you want.)

Potatoswatter
The exercise I'm doing says that I must use pIntegers as input and pOutputString as output, where pIntegers is of type int* and pOutputString is of type char*. But I understand your answer and your comment regarding iostream formatting
Juan Pablo Velasquez
A: 

Is there no bigger picture? Without knowing how pOutputString is defined, this question can't be safely answered. A char *pOutputString is only enough to hold one string representation of an integer (unless you want the string representations concatenated in one big string).

If you have a char** OutputStrings (or, better, char* OutputStrings[]), you can iterate over that just like you iterate over your pIntegers - keep in mind that each OutputStrings[i] needs to have memory allocated to it! (This is why we use std::vector and std::string in real-life code).

Is the real purpose of the exercise to write your own "integer to string representation" (Ie, your own itoa) function? If so, it's not too bad - play around with division, modulus, and adding ascii '0' :)

EDIT

Assuming you don't need to store the converted strings anywhere, have a look at the following program structure:

void IntToString(char* output, int input)
{
    // do div/mod/add-char('0') magic here, terminate buffer with \0 .
}
int main( int argc, const char* argv[] )
{
    int a[SIZE] = { /* put your contents here */ };
    char outstring[12];
    for(int i=0; i<SIZE; i++)
    {
        IntToString(outString, a[i]);
        cout << outstring << endl;
    }
}
snemarch
I updated the question with more information. You can see the code I've written so far here: http://ideone.com/nKXpg
Juan Pablo Velasquez
The purpose of the exercise is to transverse a matrix in clockwise order and print out the content of the matrix in such order.
Juan Pablo Velasquez
I'm assuming you have to write your own "int to string" conversion, otherwise you could simply printf or cout each integer... if you don't need the strings stored but only printed, you only need IntToString(char *output, int input), and you can "cout << outputbuffer;"
snemarch
A: 

I initially thought of itoa but looks like it is a non-standard function. A good first "C-ish?" attempt could be (Standard C library is not a third party lib).

int main(){
    char buf[sizeof(int)*3 + 2];
    int x = 66;

    sprintf(buf, "%d", x);
     cout << buf;
}

If the idea is to completely hand-code without any library functions, others have already provided brilliant answers.

EDIT 2 Here is another quick and dirty implementation (with dirty alternates for strlen and strrev)

int main(){
    char buf[sizeof(int)*3 + 2] = {};

    int x = 0;
    int q = 0;
    int r = 0;
    int index = 0;

    cin >> x;
    if(0 == x) 
        buf[index++] = 48;
    else{
        while(x){
            q = x / 10;
            r = x % 10;
            buf[index++] = r + 48;
            x /= 10;
        }
    }

    char *p = buf;
    size_t len = 0;

     // e.g. for 255, the buffer now has 552, need to reverse it
     // dirty replacement for strlen
    while(*p++) len++;
    len--;

     // dirty reversing logic for strrev
    for(size_t s = 0; s <= len/2; s++){
        char t = buf[s];
        buf[s] = buf[len-s];
        buf[len-s] = t;
    }
    cout << buf;
}
Chubsdad