locked
varaible conversion problems RRS feed

  • Question

  • I can't work out how to convert data types to another data type:

    eg. An int object to a char. Below is my source code:

    // rand.cpp : Defines the entry point for the console application.

    //

    #include "stdafx.h"

    #include <iostream>

    #include <fstream>

    #include <stdlib.h>

    #include <stdio.h>

    #include <time.h>

    using namespace std;

    int main( void )

    {

    int i;

    int x;

    ofstream ofsout("c:\\test.txt", ios::app);

    /* Seed the random-number generator with current time so that

    * the numbers will be different every time we run.

    */

    srand( (unsigned)time( NULL ) );

    /* Display 10 numbers. */

    for( i = 0; i < 10;i++ )

    {

    printf( " %6d\n", rand() );

    x = rand();

    ofsout.write(x, 3);

    }

    ofsout.close();

    }

    I need to convert x to a const char *, to be able to use it for the writing of the file.

    How do I do this conversion, I couldn't find anything in the std library that looked like it would fo the job for me.

    Please help. :(

    Wednesday, August 30, 2006 10:47 AM

Answers

All replies

  • Hi,
    one possible solution would be to use the function itoa. Check this example:
    http://www.cplusplus.com/ref/cstdlib/itoa.html

    In VS2005 it will probably give you a warning, you can either disable the warning or use the alternatives: _itoa or _itoa_s:
    http://msdn2.microsoft.com/en-us/library/ms235327.aspx

    Other alternative is to use the stringstream:

    string s;
    stringstream ss;
    ss << x;
    s = ss.str();
    // your char *
    char* str = s.c_str();

    Yet another one, use sprintf:
    http://www.cplusplus.com/ref/cstdio/sprintf.html

    VS2005 will also give a warning when you use this one. Check:
    http://msdn2.microsoft.com/en-us/library/ybk95axf.aspx
    Wednesday, August 30, 2006 11:37 AM
  • I decided to try and use the string stream one, but it created a few errors:

    Error 1 error C2079: 'ss' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>' c:\documents and settings\peter\my documents\visual studio 2005\projects\c++\prorpg\rand\rand\rand.cpp 31

    It wanted me to use some additional parameters to create the ss stringstream in other words, and from that it didn't accept the statment when the program tried to use the .str() conversion because the stringstream was not valid.

    Also it gave me a warning about the line:

    ss << x;

    it told me that it would not produce an effect, so I changed it to:

    ss = x;

    Is this going to do the same thing?

    Thursday, August 31, 2006 8:52 AM
  • Sorry, since I wrote that code without testing it should contain some errors (and it was incomplete since I didn't put the necessary include for the stringstream). The error you're having is because of the include that was missing, check this example:

    #include <sstream>
    #include <iostream>
    #include <string>
    using namespace std;

    void main()
    {
        int x = 10;
        string s;
        stringstream ss;
        ss << x;
        s = ss.str();
        // your char *
        const char* str = s.c_str();
        cout << str;
    }
    Thursday, August 31, 2006 9:58 AM
  • Thxs this now works. :)

    Yeah I though it may have been to do with a not included .h file after my last post!

    So thxs... :)

    Friday, September 1, 2006 9:57 PM