hello all,
I've posted an old version of the following code before but I was not clear on my question or the title of the problem [for that am sorry]

I did changes on the code now I have no errors but it does not do what I want ..

lets say there is a sentence like this "helloworld" is saved in an arry called original the unlocking charcter is "W" .. the code searches for the "w" when it finds the "w" it saves it and what is after it in a new array of charcters called first .. however before it finds the "w" it should save the letters in an array called second .. at last it refills the array original with array first and when it is done it fills it with array second ...

please see the crazy part of my code "commented as crazy part"

here is my code:

//This program takes the number of rings, the unlocking word and the secret word in the form of _T___ that is with knowing
//only one charcter of the word.
//The program then takes the cryptx (rings==lines of the cryptx)
//each line has 26 different letters
//The program should align the unlocking word and thus the secret word appears
//The program only outputs the unlocking word and the secret word
# include <iostream>
# include <string>
using namespace std;

int main()
{
	string un_lock;
	string secret;

	int rings;

	char string_in[1000][26];//[rings][letters]
	char second[1000][26];
	char first[1000][26];

	char sec_char;
	int sec_pos;
	int the_col;
	string the_secret;

	cout<<"Enter the number of the rings"<<endl;
	cin>>rings;

	cout<<"Enter the Unlocking word"<<endl;
	cin>>un_lock;

	cout<<"Enter the secret word"<<endl;
	cin>>secret;

	cout<<"Enter the cryptx"<<endl;
	for(int i=0; i<rings; i++)
		for(int j=0; j<26; j++)
			cin>>string_in[i][j];

//this is the crazy part
	for(int j=0; j<rings; j++)
	{	
	
		for(int i=0; i<26; i++)
		{ 
			
			if (string_in[j][i]!=un_lock[j] )
				second[j][i]=string_in [j][i];

			else if (string_in[j][i]==un_lock[j])
				for (int z=0, y=i; z<26-y; z++)
					first[j][z]=string_in[j][i++];	
		}
	}


		//to fill in the main string again
		for(int j=0; j<rings; j++)
		{
		int i;
			for( i=0; first[j][i]!='\0'; i++)
				string_in[j][i]=first[j][i];

			for(int p=0, z=0; second[p][z]!='\0'; z++, p++)
				string_in[j][i++]=second[p][z];
		} // end of crazy part

		
		//finding the position of the known secret letter
		//which will be the number of the ring it is located in
		for(int i=0; i<rings; i++)
			if(static_cast<int>(secret[i])!=95)
			{
				sec_char=secret[i];
				sec_pos=i;
			}

         //finding the column of the secret word
		for(int i=0; i<26; i++)
		if(static_cast<int>(string_in[sec_pos][i])==static_cast<int>(sec_char))
		the_col=i;

		//saving the secret word
		for(int j=0; j<rings; j++)
		the_secret[j]=string_in[j][the_col];


		//the output
		cout<<un_lock<<" "<<the_secret<<endl;
	

return 0;
}

I entered the number of rings : 5
the unlocking word: GREEN (ALL CAPITAL)
the secret word: _P___ (WITH UNDERSCORES)
the cryptx is as follows :
KFZLQMDWJUSHGCEIXRAOPNVTYB
IMWZPFJBKLTNOEQDHUXGVYASRC
FAMIETZORWPSQUNGLDYBKXHCVJ
XNAKVPICQHDFWEGBRTMLZOUSYJ
ZSYFDOWIJCAKPBTXLRUNGQMVHE

why does it stop after the input of the cryptx??

please help asap :(

Edited 6 Years Ago by new programer: n/a

If I understand the process correctly you want to:

//break each row in string_in in two based on the letters in the word GREEN

KFZLQMDWJUSH                            GCEIXRAOPNVTYB 
IMWZPFJBKLTNOEQDHUXGVYAS     RC 
FAMI                                        ETZORWPSQUNGLDYBKXHCVJ 
XNAKVPICQHDFW                           EGBRTMLZOUSYJ 
ZSYFDOWIJCAKPBTXLRU                 NGQMVHE 

//Store the fragments in second and first
first:   GCEIXRAOPNVTYB
           RC
           ETZORWPSQUNGLDYBKXHCVJ
           EGBRTMLZOUSYJ 
           NGQMVHE

second:   KFZLQMDWJUSH
               IMWZPFJBKLTNOEQDHUXGVYAS
               FAMI 
               XNAKVPICQHDFW
               ZSYFDOWIJCAKPBTXLRU

//recombine fragments line per line like this: first + second, to get:
GCEIXRAOPNVTYBKFZLQMDWJUSH
RCIMWZPFJBKLTNOEQDHUXGVYAS
ETZORWPSQUNGLDYBKXHCVJ FAMI 
EGBRTMLZOUSYJXNAKVPICQHDFW
NGQMVHEZSYFDOWIJCAKPBTXLRU

If that's the case then put in output statements to show string_in, first, second and the recombined lines after each task has been accomplished. Use my "pen and paper" calculations as above (or use your own) to check to see that you have what you expect.

I think the problem is likely to be how you treat second and first. Notice you do not add the null chacracter at the end of eackh row of second and first after assembly of each line therein. However, you look for the null char to terminate the loops on line 62 and 65. Since no null chars have been used so far so I susupect this causes an endless loop on line 62 becaue the null char will never be found. Thus the program appears to stop after the input of string_in since the only output statement is line 90, which will never be reached.

Edited 6 Years Ago by Lerner: n/a

you got it right .. but I didn't get how it can be fixed .. shall I add a null terminator ? and where ?

This article has been dead for over six months. Start a new discussion instead.