hello all..

I have been working on a code to deal with cryptex .. please take a look at it and tell me what am doing wrong if you may..

here's the 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 sec_len;
	int rings;
	string string_in[3][26];//[rings][letters]
	string second[3][26];
	char sec_char;
	int sec_pos;
	int the_col;
	string the_secret;
	string first[3][26];

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

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

	//the length of the word is the number of the rows/rings
	sec_len=secret.length();
	rings=sec_len;

	int f=0;
	int k=0;
	int p=0;
	int y=0;

	for(int j=0; j<rings; j++)
		for(int i=0; i<26; i++)
		{    //if the letters in the first/2nd/etc row != the letter
			//in the unlocking word save that in the second string 
			if (string_in[j][i]!=un_lock[j])
				second[f++][k++]=string_in [j][i];

			  //if the letters in the first/2nd/etc row = the letter
			//in the unlocking word save that and what is after in the first string 
			else if (string_in[j][i]==un_lock[j])
				for (int z=k; z<26; z++)
					first[p++][y++]=string_in[j][i];
		}

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

			for(int z=i; i<26; i++)
				string_in[j][i]=second[j][z];
		}

		//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<sec_len; i++)
			if(secret[i]!=static_cast<char>(95))
			{
				sec_char=secret[i];
				sec_pos=i;
			}

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

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


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

return 0;
}

if you didn't get the purpose of the code please check the attached file

Attachments
Due on: 27th of December 2009 

Question 1: This question is taken from the ACM programming competition. 

Leonardo Da Vinci, the famous inventor and artist, was fond of cryptography and has 
invented many devices and techniques to hide messages. One such invention is the 
cryptex. A cryptex is a small device used to carry a secret message and is made of one 
or more rings. Each ring has the 26 uppercase letters written in some random order. It 
is by aligning these rings in one specific way that the secret message is revealed. The 
secret message is made of two words, each of length N. The first word of the secret 
message is called the unlocking word and the second is called the secret word. To 
properly align the cryptex, you need to know the unlocking word. Once you have the 
cryptex and the unlocking word, all you have to do is align the rings on the cryptex to 
spell the unlocking word. The letters on the rings, though randomly ordered, are 
arranged in such a way that when the cryptex is aligned to spell the unlocking word, 
one of the other 25 strings would spell the secret word. To reveal the secret message 
youll need to know at least one letter from the secret word. 

Take for example the following cryptex made of five rings (each line constitutes a 

ring:) 

KFZLQMDWJUSHGCEIXRAOPNVTYB 
IMWZPFJBKLTNOEQDHUXGVYASRC 
FAMIETZORWPSQUNGLDYBKXHCVJ 
XNAKVPICQHDFWEGBRTMLZOUSYJ 
ZSYFDOWIJCAKPBTXLRUNGQMVHE 

The unlocking word is GREEN and we know that the second letter of the secret 
word is P. By aligning the rings to spell the unlocking word, the cryptex now looks 
like this: 

GCEIXRAOPNVTYBKFZLQMDWJUSH 
RCIMWZPFJBKLTNOEQDHUXGVYAS 
ETZORWPSQUNGLDYBKXHCVJFAMI 
EGBRTMLZOUSYJXNAKVPICQHDFW 
NGQMVHEZSYFDOWIJCAKPBTXLRU 

The secret word is revealed by looking for the word whose second letter is P. 


Input Format 

Your program will be tested on one or more test cases. The first line of the input 
specifies a single integer D which represents the number of test cases. Each test case 
is specified using N +1 lines. The first line of each test case has the following format: 


NUS 


N is a positive integer which is the number of rings. No cryptex will have more than 
1000 rings. U is the unlocking word while S describes the secret word. S is made of N 
characters, all underscore characters (_) except exactly one. For example, having S 
equal to "_ P _ _ _" says that the second letter of the secret word is "P". 


The remaining N lines specifies the N rings, one on each line. Each line is made of 
(different) 26 uppercase letters. 
Consecutive test cases are separated by a single blank line. 


Output Format 

For each test case, write on a separate line, the unlocking word, followed by the secret 
word, separated by a single space.
output.jpg 39.97 KB

I don't want to spend the time to figure out if your program does exactly what the spec calls for, I'd rather help you get your program to do what you expect.

What are you seeing it do that is unexpected?

(What are the symptoms)

You might want to add some temporary debug output (or run the program in a debugger) so you have a better idea of what is going on inside.

i have errors like the following :
error C2784: 'bool std::operator !=(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const _Elem *' from 'char'

c:\program files\microsoft visual studio 9.0\vc\include\string(120) : see declaration of 'std::operator !='
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(44) : error C2784: 'bool std::operator !=(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'std::string'
c:\program files\microsoft visual studio 9.0\vc\include\string(110) : see declaration of 'std::operator !='
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(44) : error C2784: 'bool std::operator !=(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'char'
c:\program files\microsoft visual studio 9.0\vc\include\string(100) : see declaration of 'std::operator !='

and others .. it's furstrating I must be doing something like syntax error that am not aware of

I did a few changes to it .. I did staic_cast from char to int and the number of errors went down to 5 ..

//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 sec_len;
	int rings;
	string string_in[3][26];//[rings][letters]
	string second[3][26];
	char sec_char;
	int sec_pos;
	int the_col;
	string the_secret;
	string first[3][26];

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

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

	//the length of the word is the number of the rows/rings
	sec_len=secret.length();
	rings=sec_len;

	int f=0;
	int k=0;
	int p=0;
	int y=0;

	for(int j=0; j<rings; j++)
		for(int i=0; i<26; i++)
		{    //if the letters in the first/2nd/etc row != the letter
			//in the unlocking word save that in the second string 
			if (static_cast<int>(string_in[j][i])!=static_cast<int>(un_lock[j]))
				second[f++][k++]=string_in [j][i];

			  //if the letters in the first/2nd/etc row = the letter
			//in the unlocking word save that and what is after in the first string 
			else if (static_cast<int>(string_in[j][i])==static_cast<int>(un_lock[j]))
				for (int z=k; z<26; z++)
					first[p++][y++]=string_in[j][i];
		}

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

			for(int z=i; i<26; i++)
				string_in[j][i]=second[j][z];
		}

		//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<sec_len; 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[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[j][the_col];


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

return 0;
}

the remaining 5 errors are:

c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(44) : error C2440: 'static_cast' : cannot convert from 'std::string' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(49) : error C2440: 'static_cast' : cannot convert from 'std::string' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(76) : error C2226: syntax error : unexpected type 'std::string'
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(76) : error C2144: syntax error : 'std::string' should be preceded by '('
c:\users\documents\visual studio 2008\projects\q1\q1\q1.cpp(81) : error C2275: 'std::string' : illegal use of this type as an expression
c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string

any help/guidence is highly appreciated

string_in[j][i]!=un_lock[j]

un_lock is a string, and un_lock[j] is a 'char' at position 'j'. while string_in[j][j] is a string. hence the error. You are trying to compare a string with a char and the compiler tells you that it is not allowed.

You're 2nd attempt just changes the error because you do a non-allowed static_cast from string to int. it has not solved any problem. The compiler, on both the occasions, clearly tells you what the problem is, but you're not reading the compiler output carefully. Simply shooting in the dark won't help.

from your comment, it seems that you need to compare each string in the array with the un_lock string and for that you could just do

string_in[j][i]!=un_lock
This article has been dead for over six months. Start a new discussion instead.