Concatenating various datatype variables into another variable

This is a discussion on Concatenating various datatype variables into another variable within the C Programming forums, part of the General Programming Boards category; Hi, I'm only a few days new to C programming as well as programming in general and am trying to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    Question Concatenating various datatype variables into another variable

    Hi,

    I'm only a few days new to C programming as well as programming in general and am trying to write a program that generates a random password that is 2 characters, 3 numbers and 2 characters. I am able to get the output into a printf statement, am not able to get the variables to concatenate into one. I keep getting pointer without a cast errors

    What I have so far is below & any help would be greatly appreciated. (I'm using Cygwin, but the target for this will be SCO Unix 7.1 (Unixware) with C.)

    Thanks,

    Sam

    -------------------------------
    #include <stdio.h>
    #include <time.h>
    #include <strings.h>

    int main()
    {
    int p, a, s, w;
    int num;
    char p1, p2, p3, p4;
    char password[7];

    srand(time(0));
    p = 98 + rand()%25;
    p1 = (char)p;

    srand(time(0)+1);
    a = 98 + rand()%25;
    p2 = (char)a;

    srand(time(0)+2);
    s = 98 + rand()%25;
    p3 = (char)s;

    srand(time(0)+3);
    w = 98 + rand()%25;
    p4 = (char)w;

    srand(time(0));
    num = 100 + rand()%900;

    strcpy(password, p1);
    strcat(password, p2);
    strcat(password, num);
    strcat(password, p3);
    strcat(password, p4);

    printf ("%c%c%d%c%c\n", p1, p2, num, p3, p4); /*testing only, really want output in password variable for use by another program*/

    printf ("%c", password);

    return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just use the sprintf function:

    Code:
    char password[8]; /* need extra space for  \0 character */
    
    sprintf(password, "%c%c%d%c%c\n", p1, p2, num, p3, p4);
    
    printf("%s\n", password);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Small correction: the \n in the sprintf function in my previous post must be removed.

    Here a reduced version:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
       char password[8];
    
       srand((unsigned)time(NULL));
    
       sprintf(password, "%c%c%.3d%c%c", 
              98+rand()%25, 
              98+rand()%25, 
              rand()%1000, 
              98+rand()%25, 
              98+rand()%25);
    
        printf ("%s", password);
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    2
    Thanks Monster!

    I appreciate your time and effort to help a stranger. I see from what you wrote, that as one gets more comfortable with C, the level of meaningful context for the code can become very compact.

    -Sam

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    No problem Sam, we enjoy helping strangers...

    Once you get more comfortable with C you'll see that there are many different ways to solve your problem. Finding the best way is what makes you a good programmer (I think).

    Making your code compact can be nice, but making it too compact can make your core realy messy and unreadable by other programmers. The chalenge is to find the perfect balance between those two.

    Cheers,
    Monster

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Wandering slightly OT for this thread, but I mentioning it anyway... don't forget that this method of getting random numbers isn't very good:
    rand()%1000

    Read it here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Variable Call via Another Variable's Content
    By E-Rac in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2005, 12:51 PM
  3. newb question~
    By Anima in forum C++ Programming
    Replies: 10
    Last Post: 08-24-2003, 05:54 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21