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

Recommended Answers

All 4 Replies

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.