Checking to see if the value has looped?

This is a discussion on Checking to see if the value has looped? within the C++ Programming forums, part of the General Programming Boards category; Code: for (z=0; z <= iLength; z++) { for (i=0; i <= 6; x=x+6, i++, y++) { //cout << char( ...

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

    Checking to see if the value has looped?

    Code:
    	for (z=0; z <= iLength; z++)
    	{
    		for (i=0; i <= 6; x=x+6, i++, y++)
    		{
    			//cout << char( bin_dec(binnum) ) << endl;
    			sixbit[0] = sOutput3[0+x];
    			sixbit[1] = sOutput3[1+x];
    			sixbit[2] = sOutput3[2+x];
    			sixbit[3] = sOutput3[3+x];
    			sixbit[4] = sOutput3[4+x];
    			sixbit[5] = sOutput3[5+x];
    			sixbit[6] = '\0';
    			strcpy(sixbit2,sixbit);
    //			s64bitenc[y] = char( bin_dec2(sixbit,sixbit2) );
    	
    			iLoopTest1 = bin_dec2(sixbit,sixbit2);
    			
    			if (iLoopTest1 != iLoopTest2)
    			{
    				strcat(s64bitenc,itoa(bin_dec2(sixbit,sixbit2),sixbit2,10)); 
    				strcat(s64bitenc," ");
    
    				iLoopTest2 = iLoopTest1;
    				iLoopTest1 = -5;
    			}
    			else
    			{
    				break;
    				break;
    			}
    
    		}
    	}
    If I use it without the if and the else

    instead of:
    Code:
    			if (iLoopTest1 != iLoopTest2)
    			{
    				strcat(s64bitenc,itoa(bin_dec2(sixbit,sixbit2),sixbit2,10)); 
    				strcat(s64bitenc," ");
    
    				iLoopTest2 = iLoopTest1;
    				iLoopTest1 = -5;
    			}
    			else
    			{
    				break;
    				break;
    			}
    I use
    Code:
    				strcat(s64bitenc,itoa(bin_dec2(sixbit,sixbit2),sixbit2,10)); 
    				strcat(s64bitenc," ");
    Like I did before I added the ifs, then I get this for output if the input was "0100100001100101011011000110110001101111":
    18 6 21 44 27 6 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    for output.

    Using the if, 18 6 21.

    The desired output for what I mentioned up there would be "18 6 21 44 27 6 15" by converting the binary value of every 6 characters into a number.

    How do I get rid of all those zeros, easily?

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Code:
    while(s64bitenc[i] != '0')
      printf("%c", s64bitenc[i]);
    That should work if what you want to output is stored within a char array or string. Hard to tell from the code.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Yeah, but I want it stored without the 0s, not just outputted.

    And I'm thinking that it may come to "000000" as a binary value, in which case that would convert to 0, and I nead it read...

    Therefore, I need to know exactly why it puts all the extra zeros, and then I could fix it. But I really have no idea other than the fact that

    Code:
    	for (z=0; z <= iLength/(134/6)+1; z++)
    	{
    		for (i=0; i <= 6; x=x+6, i++, y++)
    		{
    			//cout << char( bin_dec(binnum) ) << endl;
    			sixbit[0] = sOutput3[0+x];
    			sixbit[1] = sOutput3[1+x];
    			sixbit[2] = sOutput3[2+x];
    			sixbit[3] = sOutput3[3+x];
    			sixbit[4] = sOutput3[4+x];
    			sixbit[5] = sOutput3[5+x];
    			sixbit[6] = '\0';
    			strcpy(sixbit2,sixbit);
    //			s64bitenc[y] = char( bin_dec2(sixbit,sixbit2) );
    	
    			strcat(s64bitenc,itoa(bin_dec2(sixbit,sixbit2),sixbit2,10)); 
    			strcat(s64bitenc," ");
    
    		}
    	}
    	s64bitenc[y] = '\0';		//remove trailing garbage characters
    
    	cout << s64bitenc << endl;
    That gets rid of all the zeros, except one, which I think is caused by the fact I have:

    for (i=0; i <= 6; x=x+6, i++, y++)

    note the i <= 6. That would explain it, but I haven't tested if it would work without 0100100001100101011011000110110001101111 as the input.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I just tested it using 01001000011001010110110001101100011011110100100001 100101011011000110110001101111 as the value, and it outputs almost half of what it should:

    18 6 21 44 27 6

    when it should have outputted:

    18 6 21 44 27 6 15 18 6 21 44 27 6 15... so that handling mentioned above wouldn't always work.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    It gives 18 6 21 44 27 6 61 8 25 22 305 44 27 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0

    if I remove the for loops and the handling for all the zeros.

    So there seems to be a set number of zeros based on how I set it up, since there is double the number of zeros. That was with using the binary values for "Hello" twice so the binary for "HelloHello" or "0100100001100101011011000110110001101111010010000 1100101011011000110110001101111"

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Silly question, Trauts.

    Where are 'x' and 'y' initialized?

    Interesting problem, but with only a (big) snippet of your code, I'm not sure we can really offer anything other than "guesses".

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Right... here's where they're initialized

    here's the beginning of the "scramble()" function
    Code:
    	char sInput[8192];
    	char sOutput[1024];
    	char sOutput2[4096];
    	char sOutput3[4096];
    	int iLoopTest1;
    	int iLoopTest2;
    	char s64bitenc[8192];
    	char sixbit[6];
    	char sixbit2[6];
    	char key[100];
    	const char sAlphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\0";
    	int iRotate = 0;
    	int iLength = 0;
    	int iBuffer = 0;
    	int i = 0, x = 0, y = 0, z = 0;
    Then, just before the for loop, there is this:
    Code:
    	i = x = y = z = 0;

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Does anyone know what I can do to fix it?

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I'll post enough code to compile it around monday or tuesday. I'm going out of town, so I can't do it until then.

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Code:
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    scramble();
    return 0;
    }
    void scramble()
    {
    	char sInput[8192];
    	char sOutput[1024];
    	char sOutput2[4096];
    	char sOutput3[4096];
    	int iLoopTest1;
    	int iLoopTest2;
    	char s64bitenc[8192];
    	char sixbit[6];
    	char sixbit2[6];
    	char key[100];
    	const char sAlphabet[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\0";
    	int iRotate = 0;
    	int iLength = 0;
    	int iBuffer = 0;
    	int i = 0, x = 0, y = 0, z = 0;
    
    	cout << "Put anything you want to encrypt in the file encryption.txt\n";
    	infile.open("encryption.txt",ios::in);
    	
    	strcpy(sInput,"\0");
    
    	while (!infile.eof())
    	{
    		infile.getline(sInput, 8192, '\0'); // read up to the null terminator OR 8192 chars;
    		//infile >> sBuffer;
    		//strcat(sInput,sBuffer);
    	}	
    	infile.close();
    
    	cout << endl << sInput << endl << endl;
    	
    //code goes here
    	strcpy(sOutput3,sInput); //this is so you don't need the whole code
    
    	iLength = strlen(sOutput3);
    
    	i = x = y = z = 0;
    
    	strcpy(sOutput3,"0100100001100101011011000110110001101111");
    	iLength = strlen(sOutput3);
    	
    	strcpy(s64bitenc,"\0");		//remove leading garbage characters
    
    
    //	for (z=0; z <= iLength/(134/6)+1; z++)
    	for (z=0; z <= iLength; z++)
    	{
    		//for (i=0; i <= 6; x=x+6, i++, y++)
    		for (i=0; i <= 6; x=x+6, i++, y++)
    		{
    			//cout << char( bin_dec(binnum) ) << endl;
    			sixbit[0] = sOutput3[0+x];
    			sixbit[1] = sOutput3[1+x];
    			sixbit[2] = sOutput3[2+x];
    			sixbit[3] = sOutput3[3+x];
    			sixbit[4] = sOutput3[4+x];
    			sixbit[5] = sOutput3[5+x];
    			sixbit[6] = '\0';
    			strcpy(sixbit2,sixbit);
    //			s64bitenc[y] = char( bin_dec2(sixbit,sixbit2) );
    	
    			iLoopTest1 = bin_dec2(sixbit,sixbit2);
    			
    			if (iLoopTest1 != iLoopTest2)
    			{
    				strcat(s64bitenc,itoa(bin_dec2(sixbit,sixbit2),sixbit2,10)); 
    				strcat(s64bitenc," ");
    
    				iLoopTest2 = iLoopTest1;
    				iLoopTest1 = -5;
    			}
    			else
    			{
    				break;
    				break;
    			}
    
    		}
    	}
    	s64bitenc[y] = '\0';		//remove trailing garbage characters
    
    	cout << s64bitenc << endl;
    
    	outfile.open("encryption.txt",ios::out);
    	outfile << s64bitenc;
    	outfile.close();
    	
    // 1 2 3 4 5
    // 1 3 5 2 4
    }	//end of encrypt function
    
    
    int bin_dec2(char sBinvalue[6],char sBinvalue2[6])
    {
    	long unsigned int iDecNum = 0;
    			char sBinChar[8];
    
    			sBinChar[0] = '0';
    			sBinChar[1] = '0';
    			sBinChar[2] = sBinvalue[0];
    			sBinChar[3] = sBinvalue[1];
    			sBinChar[4] = sBinvalue[2];
    			sBinChar[5] = sBinvalue[3];
    			sBinChar[6] = sBinvalue[4];
    			sBinChar[7] = sBinvalue[5];
    			sBinChar[8] = '\0';
    
    //		itoa(iBinvalue,sBinvalue,8);
    
    		_strrev(sBinChar);
    
    		if (sBinChar[0]=='1')
    		{ iDecNum = iDecNum + 1; }
    		if (sBinChar[1]=='1')
    		{ iDecNum = iDecNum + 2; }
    		if (sBinChar[2]=='1')
    		{ iDecNum = iDecNum + 4; }
    		if (sBinChar[3]=='1')
    		{ iDecNum = iDecNum + 8; }
    		if (sBinChar[4]=='1')
    		{ iDecNum = iDecNum + 16; }
    		if (sBinChar[5]=='1')
    		{ iDecNum = iDecNum + 32; }
    		if (sBinChar[6]=='1')
    		{ iDecNum = iDecNum + 64; }
    		if (sBinChar[7]=='1')
    		{ iDecNum = iDecNum + 128; }
    		if (sBinChar[8]=='1')
    		{ iDecNum = iDecNum + 256; }
    
    
    		//cout << iDecNum << endl;	//cout the binary number in decimal
    		return iDecNum;				//return the value from the function
    }
    Last edited by Trauts; 11-14-2002 at 08:47 AM.

  11. #11
    Tincan
    Guest

    ANYONE?

    This puzzles me as well. Maybe combine the

    for (z=0; z <= iLength; z++)
    {
    for (i=0; i <= 6; x=x+6, i++, y++)

    into one for loop? Can anyone find a way to do that?

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Tincan has a point...

    for (z=0; z <= iLength; z++)
    {
    for (i=0; i <= 6; x=x+6, i++, y++)

    if that was one for loop, getting rid of the i, then there wouldn't be a problem with it looping... I could change just one value.

    Any ideas? maybe...

    for (z=0; z <= iLength*6; z++, y++)

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Please, can someone help me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

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