Help - Search - Members - Calendar
Full Version: C++ Help
ieXbeta Board > Tech > Developer Center
Chris
Hey guys hows it going? I have a little problem for you.

This code works fine, but as you can see it is very untidy is there any way of getting this into a loop?

cheers

CODE
void stringfun()
{

char l_OrgBuf[201];
char l_Buf1[100];
char l_Buf2[100];
char l_Buf3[100];
char l_Buf4[100];
char l_Buf5[100];
char l_Buf6[100];
char l_Buf7[100];
char l_Buf8[100];

//upto char l_buf40[200];
//.... etc...




strcpy(l_OrgBuf,crpyted);
strncpy(l_Buf1,l_OrgBuf,5);
l_Buf1[5]='\0';
strncpy(l_Buf2,l_OrgBuf+5,5);
l_Buf2[5]='\0';
strncpy(l_Buf3,l_OrgBuf+10,5);
l_Buf3[5]='\0';
strncpy(l_Buf4,l_OrgBuf+15,5);
l_Buf4[5]='\0';
strncpy(l_Buf5,l_OrgBuf+20,5);
l_Buf5[5]='\0';
strncpy(l_Buf6,l_OrgBuf+25,5);
l_Buf6[5]='\0';
strncpy(l_Buf7,l_OrgBuf+30,5);
l_Buf7[5]='\0';
strncpy(l_Buf8,l_OrgBuf+35,5);
l_Buf8[5]='\0';
strncpy(l_Buf9,l_OrgBuf+40,5);
l_Buf9[5]='\0';
strncpy(l_Buf10,l_OrgBuf+45,5);
l_Buf10[5]='\0';
strncpy(l_Buf11,l_OrgBuf+50,5);
l_Buf11[5]='\0';
strncpy(l_Buf12,l_OrgBuf+55,5);
l_Buf12[5]='\0';
strncpy(l_Buf13,l_OrgBuf+60,5);
l_Buf13[5]='\0';
strncpy(l_Buf14,l_OrgBuf+65,5);
l_Buf14[5]='\0';
strncpy(l_Buf15,l_OrgBuf+70,5);
l_Buf15[5]='\0';
strncpy(l_Buf16,l_OrgBuf+75,5);

//etc...
//upto
//strncpy(l_Buf40,l_OrgBuf+205,5);
//l_Buf40[5]='\0';

cout << "\n\n";
cout << "Encrypted Text:";
cout << "\n";


printf("Original : %s\n",l_OrgBuf, "\n");

cout << "\n";

cout << l_Buf1;
cout << " ";
cout << l_Buf2;
cout << " ";
cout << l_Buf3;
cout <<" ";
cout << l_Buf4;
cout <<" ";
cout << l_Buf5;
cout <<" ";
cout << l_Buf6;


// etc...
//upto cout << l_buf40;

}
Sinbad
How about an array of arrays? Not 100% sure you can do that in C++, can in Java, but 99% sure you can in C++ as well.
Chris
Yeah not too sure on how to do that though? I had a play but couldn't get anything to work.
Sinbad
Um can you describe a little what the program is trying to do? Or a sample of what input/output should be like?

Seems like some sort of rotating cryptography, but I'm too tired to be sure. Oh, and what is crpyted? More what's its value, cause I see it referenced where you start doing strncpy, but it's not initialized anywhere.
Chris
ok the function is getting an encrypted phrase (crypted) and splitting it into groups of 5, so if say crypted was dfghfsewebdfghiohfgf then this function would make that into dfghf seweb dfghi ohfgf.

Hope this helps
Sinbad
So basically you are just taking some string and splitting it up every 5th char?
Chris
yup..
Sinbad
CODE
#include <iostream>
#include <string>

using namespace std;

void main () {
    const int SIZE = 50;
    const int SPLITSIZE = 5;

    // array for storing our subsections
    string cryptedArray[SIZE];

    // some sample crypted string
    string crypted = "abcdefjklmnopqrstuvwxyz12345678ksjfhkjsdhfjksdhfjhsdffldsfhdskjfhsdkjfhsd0";

    int subStrIndex = 0;

    // some info about original string
    cout<<"Crypted Length: "<<crypted.length()<<endl;
    cout<<"Crypted Original: "<<crypted<<endl;

    // don't want to run for every char of the crypted string, just every SPLITSIZE
    int iters = (crypted.length()/SPLITSIZE);
    if (crypted.length() % 5)
        iters++;
  
    for (int i = 0; i<iters; i++){
        // for each index of array, get a substring of the crypted string, then move index by SPLITSIZE
        cryptedArray[i] = crypted.substr(subStrIndex, SPLITSIZE);
        subStrIndex += SPLITSIZE;
    }
    
    for (int j = 0; j<iters; j++){
        cout<<cryptedArray[j]<<' ';
    }
    cout<<endl;
}



Couple things.. when you are making arrays, you should make a const variable for the size. In your example, you had 100 for everyone of them. What if you wanted to change that value? Instead of changing 40 of them or whatever, you could change just one.

Also, whenever you have a lot of data, especially sequential data, and data of the same type, you should use an array. Array of arrays, array of strings, anything. Else you won't be able to do any looping, like you were trying to do. smile.gif
Chris
Thanks very much smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.