Thread: making an int a char string

  1. #1
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66

    making an int a char string

    i need help with this program i am currently working on.
    it invloves 7 random numbers (made into a character string) that could potentially be used for something like a password for instance. example: say the random numbers come up as 1 4 7 6 3 2 8. i want a character string that says: 1476328.
    i tried using strcpy and strcat as well as dabbled in atoi none of which helped me.
    does anyone know how to do this?
    thanks for your help.

  2. #2
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    i can post an example of code if needbe...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could insert the numbers to a stringstream, and then extract from the stringstream to a string:
    Code:
    std::string password;
    const int numbers_size = 7;
    int numbers[numbers_size] = {1, 4, 7, 6, 3, 2, 8};
    std::stringstream ss;
    std::copy(numbers, numbers + numbers_size, std::ostream_iterator<int>(ss));
    ss >> password;
    Note that you'll need the <algorithm>, <iterator>, <string> and <sstream> standard headers included for my example to work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    boost::lexical_cast might just be what you need.

    Definitely more intuitive than strstream if not as efficient

    The code would be something like:
    Code:
    int numbers[7] = {1, 4, 7, 6, 3, 2, 8};
    
    string out = boost::lexical_cast<string>(numbers[0]) + boost::lexical_cast<string>(numbers[1]) + boost::lexical_cast<string>(numbers[2]) + boost::lexical_cast<string>(numbers[3]) + boost::lexical_cast<string>(numbers[4]) + boost::lexical_cast<string>(numbers[5]) + boost::lexical_cast<string>(numbers[6]);
    Of course you coulde also do it in a loop
    Code:
    int numbers[7] = {1, 4, 7, 6, 3, 2, 8};
    string out;
    
    for (int i = 0; i < 7; ++i)
    {
            out += boost::lexical_cast<string>(numbers[i]);
    }
    you can find the boost libraries at www.boost.org

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You could just create a lookup table and use the random number as an index into the string.
    Code:
    string table="0123456789";
    string s;
    
    s+=table[rand()%10];
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    There is a function called atoi() which converst array to integer. It fullfills posix.1 standard, so it HOULD be a part of the standard libraries included in any serious compiler... Unfortunately M$ is not too known of staying within standards... (Well, I think M$ compilers do have that included too though, and gcc definitely has ) Only problem with atoi is, that it does not inform about errors... If there's some lets say alphabets in array, atoi will just return 0.

    EDIT:
    If you need error reporting, use strtol instead

  7. #7
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Oh, sorry. I missread you. atoi and strtol work in opposite way :|

    Well, there is function called itoa(), but it is not included in posix/ansi standards as far as I know... Anyways some compilers do include it in their standard libraries. I also found quite a good one when I had no time to make my own, by using our dear friend google

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    atoi() is part of the ANSI C standard and thus present in every hosted C implementation, i.e. every compiler a new programmer is every likely to encounter.

    For a straight C way to solve the given problem, there's snprintf (C99, so older compilers like MS's might not have it - MS has something like it, though, you have to look it up.)
    Code:
    #include <string.h>
    
    #define BUFSIZE 100
    
    int main()
    {
      int numbers[] = { 2, 3, 4, 1, 6, 7, 5 };
      char buffer[BUFSIZE];
    
      snprintf(buffer, BUFSIZE, "%i%i%i%i%i%i%i", numbers[0], numbers[1] ...);
    }
    Last edited by CornedBee; 03-29-2006 at 06:53 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>If there's some lets say alphabets in array, atoi will just return 0.

    Well, not really. atoi() first skips over all white space (spaces, tabs) then converts up to the first non-numeric digit, then returns whatever value it has converted up to that point. Although it is documented to return 0 on error I have never encountered that problem. The main problem with atoi() is that it does not detect data overflow.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could just create a lookup table and use the random number as an index into the string.
    The problem is that the OP didnt specify that the random numbers will always be in the range [0, 9]

    EDIT:
    if we did make that assumption, I suppose my example could be modified to:
    Code:
    char int2digit(int n) {
    	return static_cast<char>(n) + '0';
    }
    
    // ...
    
    std::string password;
    const int numbers_size = 7;
    int numbers[numbers_size] = {1, 4, 7, 6, 3, 2, 8};
    std::transform(numbers, numbers + numbers_size, std::back_inserter(password), int2digit);
    Last edited by laserlight; 03-29-2006 at 09:47 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    well i was told atio could be used but i could not make it work. i tried using the strcpy and strcat wich obviously didnt work but i suppose it was worth a try... ill post an example of what i have

  12. #12
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    here was an attempt using the strcat which does not work since you cant convert an int to a character string.

    Code:
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    #include <string>
    
    using namespace std; 
    
    int main() 
    { 
    srand((unsigned)time(0));
     
    int random_integer, x=0, n1, n2, n3, n4, n5, n6, n7;
    char pass[7];
     
    do
    { 
    random_integer = (rand()%9)+1; 
    cout << random_integer;
    x++;
    
    if (x==1)
    {
    n1=random_integer;
    } 
    
    if (x==2)
    {
    n2=random_integer;
    }
    
    if (x==3)
    {
    n3=random_integer;
    }
    
    if (x==4)
    {
    n4=random_integer;
    }
    
    if (x==5)
    {
    n5=random_integer;
    }
    
    if (x==6)
    {
    n6=random_integer;
    }
    
    if (x==7)
    {
    n7=random_integer;
    }
    }
    while (x!=7);
    
    cout << endl << n1 << n2 << n3 << n4 << n5 << n6 << n7;
    
    strcat(pass,n1);
    strcat(pass,n2);
    strcat(pass,n3);
    strcat(pass,n4);
    strcat(pass,n5);
    strcat(pass,n6);
    strcat(pass,n7);
    
    getch();
    }

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Code:
    Just Sorry..
    Last edited by Banzai10; 03-30-2006 at 01:02 PM. Reason: Wrong code again

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    here was an attempt using the strcat which does not work since you cant convert an int to a character string.
    There are a number of suggestions in this thread, have you not looked at them?

    Banzai10, your example has a terrific bug with it, but you cant see it since you deceived yourself by printing the original integers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    my output was very strange. maybe i did it incorrectly but it did run...

    Code:
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    #include <string>
    
    using namespace std; 
    
    int main() 
    { 
    srand((unsigned)time(0));
     
    int random_integer, x=0, n1, n2, n3, n4, n5, n6, n7;
    char pass;
    string out;
     
    do
    { 
    random_integer = (rand()%9)+1; 
    cout << random_integer;
    x++;
    
    if (x==1)
    {
    n1=random_integer;
    } 
    
    if (x==2)
    {
    n2=random_integer;
    }
    
    if (x==3)
    {
    n3=random_integer;
    }
    
    if (x==4)
    {
    n4=random_integer;
    }
    
    if (x==5)
    {
    n5=random_integer;
    }
    
    if (x==6)
    {
    n6=random_integer;
    }
    
    if (x==7)
    {
    n7=random_integer;
    }
    }
    while (x!=7);
    
    out += (char)n1;
    out += (char)n2;
    out += (char)n3;
    out += (char)n4;
    out += (char)n5;
    out += (char)n6;
    out += (char)n7;
    
    cout << out;
    
    cin.get();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  3. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM