Cipher Problem....Help

This is a discussion on Cipher Problem....Help within the C Programming forums, part of the General Programming Boards category; Hi I need some help with this function I wrote.....I have multiple variables of the same type..eg laintext1,plaintext2....plaintext100. I want ...

  1. #1
    Unregistered
    Guest

    Question Cipher Problem....Help

    Hi I need some help with this function I wrote.....I have multiple variables of the same type..eglaintext1,plaintext2....plaintext100. I want to cycle through each one and process them...
    I wrote the following while loop...It only works for two...

    while(j<=2)
    {
    char str1[11] ={"plaintext"};
    char str[3];

    str[3]=itoa(j,str,10);

    strcat(str1,str);
    printf("%s values are\n",str1);

    split_Data(str1,ciphertext1);
    }
    so I have a string initialised with Plaintext..and I concatenate the loop variable each time and call split_data...
    now my problem is...the first arg (the concatenated variable I spoke about above) value is the string....not the actual variables I have...
    any ideas...I would really appreciate some help on this...
    thanks

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    If I understand your problem right, you are trying to concatenate non-string variables to a string. If this is correct, I would suggest using sprintf(). It works exactly as printf(), with the difference that the first parameter is the string to print to.

    char message[] = { "Hello World!" };

    printf("this is my message: %s.\n", message);

    or

    char buffer[64];
    char message[] = { "Hello World!" };

    sprintf(buffer, "this is my message: %s.\n", message);

    printf("%s", buffer);

    Both produce the same result. The advantage it gives you is that you can print any type of variable to a string. And then you can concatenate that string any way you want. I think you got the point...
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think what they want is:
    Code:
    myfunction( char *s )
    {
        int var1, var2, var3;
    
        if( s == nameof( var1 ) ) ...
        if( s == nameof( var2 ) ) ...
    }
    Something like that?

    You could probably use the string-ize preprocessor: ## to do this, then combine it with strcmp()

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay... so you're saying you have 100 different variables, and you want a fairly automated way to process every one of them?

    This cannot be done... unless your compiler has some very very obscure macroing commands, and even then, it won't be done very well.

    Basically, this is the rule that should illuminate it...
    Code:
    char * variableName = "IAmVariable";
    int IAmVariable;
    There is no way to access IAmVariable using the string, variableName. It is impossible, even though the string is the name of the variable. Variable names are only meaningful at compile time, and character arrays are only meaninful at run time. There isn't any interaction between the two (well, this statement might be a bit broad, but it's largely correct).

    You really need to use an array of variables... instead of using...
    Code:
    //100 ints...
    int plaintext1;
    int plaintext2;
    int plaintext3;
    // and so on...
    int plaintext99;
    int plaintext100;
    You should use...
    Code:
    //An array of 100 ints...
    int plaintext[100];
    Also....
    Code:
    // In the former method, you would access an int as...
    x = plaintext1;
    // Using an array, it's very similar....
    x = plaintext[0];
    It also makes it very easy to deal with each variable.. the code would look like this...
    Code:
    while(j<=2) 
    { 
     // This is a little bit of printf vodoo to get the same output
     printf("plaintext%d values are\n", j + 1); 
    
     split_Data(plaintext[j],ciphertext1);
     j++; // You left this out, and I'm not sure why...
    
    }
    Mmmm, 3 lines.

    In my examples, I assumed that the variables are of type int. I realise that more than likely, you are handling something different, but I really just don't know.

    This is a fairly backwards explanation of arrays which i've put up here, so it is perhaps not so good for understanding. If you need it, I may provide a more complete explanation, although any explanation the people here might muster probably isn't on par with what you would find in a C Programming text. If you have one, check the section in your text on arrays.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

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