-
Decoding in arrays
If anybody could help me I would appreciate it.
I am trying to decode using the following:
Key string:DOCTRZSIENVWABFGHJKLMPQUXY
Decode string: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Infile string: CDMLEFB LIR AFMBLDEBK DJR ZMWW FZ IEWWK
Infile string should decode to: CAUTION THE MOUNTAINS ARE FULL OF HILLS
What I have been trying to do is find the position of the incoming letters from the file. My problem is that I only get the first letter of the infile (C being at position 2, which equals to C, the next one should be D at position 0, which equals to A, etc). If I add an index to "final", it gives me a compiler error. Maybe I'm going at it the wrong way? Please help!
char str[80];
char string3[27] = "DOCTRZSIENVWABFGHJKLMPQUXY";
char final[80];
int i;
char *pdest;
int result;
void main(void)
{
cls;
infile.open ("F1949.txt", ios::in);
while (infile)
{
infile.get (final[i]);
cout << final[i];
pdest = strstr( string3, final );
result = pdest - string3;
if( pdest != NULL )
{
cout << endl << final[i] << " found at position " << result << endl;
}
i++;
}
}
-
The key string should be
DOCTRZSIENVWABFGHJKLMPQUXY
I don't know how that smiley face got there.
-
Well, I didn't get any answers from this post but by doing more research, I got it to work. I would like to thank everybody who took the time to take a look at my post.
If anyone wishes to add any comments on this code, like if it can be improved, please feel free to do so. It would actually be very much appreciated.
char str[80];
char string3[28] = "DOCTRZSIENVWABFGHJKLMPQUXY ";
char decode[28] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char final[80];
int i, result;
char *pdest;
void main(void)
{
cls;
infile.open ("F1949.txt", ios::in);
cout << "The decoded phrase is:" << endl << endl;
while (infile)
{
infile.get (final[i]);
pdest = strchr( string3, final[i] );
result = pdest - string3;
if( pdest != NULL )
{
cout << decode[result];
} // END IF
i++;
} // END WHILE
cout << endl << endl;
} // END MAIN
-
Avoid global variables when possible, they destroy the idea of data hiding and encapsulation.
>void main(void)
main returns an int, nothing else.
>cls;
What is this?
>while (infile)
This is an illegal construct, I believe you meant something more like:
while ( infile.good())
-Prelude
-
Thanks for the tips! It is very much appreciated. I will adjust the program correctly.
>void main(void)
>main returns an int, nothing else.
I know, but that is how teacher wants it.
>cls;
>What is this?
I should of included the whole thing. It's a command to clear the output screen whenever it is needed. Done by adding:
#define cls system ("cls")
I just didn't want to overpost. It's another one of the teacher's request. But while we are on the subject, our teacher makes us type:
#include "a:bcc.h"
Where on a:\ ALL the #include are stored, hence no need to type them everytime. Do you think it's a good idea? I mean I have to do it for now, but should I keep doing this in the future or do you think it's bad programming?
Thanks again!
-
>I know, but that is how teacher wants it.
Teacher is a moron then
>#define cls system ("cls")
Not only is the practice of redefining something simple like system ( "CLS" ) a practice in obfuscation, the system function is godawful slow and should be avoided like the plague when possible.
>Do you think it's a good idea?
No, if you have a specialized header that only includes standard headers you'll do nothing but annoy readers. I can usually tell if a program will be able to compile on my system by reading the headers, if they aren't there then I'll assume the program won't compile and move on.
>I mean I have to do it for now
No you don't, tell your teacher he/she is wrong and why. Especially with void main, an instructor that insists you use it is of poor quality and shouldn't be teaching anyway.
-Prelude
-
I'm laughing so hard I'm crying!!
He is a moron, but he is very set in his ways. I'm getting an A in the class up to now and I want to keep it that way. I know it sucks and I should rebel, but I've seen what happens to people who disagree with him. Old fart. Hence, I'm just going to do what I am told for now keeping in mind that the way he wants things isn't the proper practice. And if I'm not sure of something, I'll ask the pros.
Hell, he doesn't teach anything anyway, we pretty much have to figure out everything on our own. More than half of the students have dropped the class because they couldn't keep up. That sure tells ME that something is wrong with the teacher!!
By the way, I found the URL's in your signature very informative.