Decrypt C++ Issues

Started by
6 comments, last by Zahlman 15 years ago
a buddy of mine and i were working on a decrypt program that reads a key then stores it into an array, from there it reads a line of the encrypted message and decrypts it, we have gotten this far could someone help us and point us in the right direction to finish it? somethings up with our sringlength setup in the test in the void function. any input is appreciated #include <iostream> #include <fstream> #include <iomanip> #include <cstring> using namespace std; const int MAX = 26; istream & fill_array(istream & is, char a[],int MAX, int numUsed); void decrypt(char lc[], char key[], char dcc[]); int main( ) { cout << "Lab8 \t\t\t Robert Lury \n\n"; char key[MAX], lc[151], dcc[151]; int number_used; ifstream fin; fin.open("key.txt"); if (fin.fail()) { cerr << "Error opening the input file" << endl; exit(1); } fill_array(fin, key, MAX, number_used); fin.close(); ifstream fin2; fin2.open("encrypt.txt"); if (fin2.fail()) { cerr << "Error opening the input file" << endl; exit(1); } while (fin2.getline(lc,151)) { decrypt(lc, key, dcc); cout << dcc << endl; } fin2.close(); system("pause"); return(0); } istream & fill_array(istream & is, char a[],int MAX, int numUsed) { double next; int i = 0; for(int i = 0; i < MAX; i++) is >> a; return is; } void decrypt(char lc[], char key[], char dcc[]) { int ioc; for (int k = 0; k < str.length(lc); k++) { if(lc[k] == ' ') dcc= ' '; else ioc = search(key, 26, lc[k]) dcc[k] = char(int('a') + ioc); dcc[strlength(lc)] = '\o'; } } [code/]
Advertisement
Quote:Original post by dodger_404
void decrypt(char lc[], char key[], char dcc[]){    int ioc;    for (int k = 0; k < str.length(lc); k++)    {...


You don't have a str object here. Are you trying to use a std::string? Or the C std library function strlen?

Also, we can't really do your homework for you here. It's fine to ask questions about it, but make them specific questions that show us you've put some effort into writing and debugging your current work. For example, what have you done so far for this assignment? Did you give up when trying to compile it? Is it running but giving unexpected output? If so, what have you done to debug why the output is incorrect?
We can't do your homework for you. But we can give you hints.

My hint: you are trying to use two undefined functions, strlength and search(). You also tried str.length(). I think you are looking for the function strlen(). You will have to declare and implement search() by yourselves.

Assuming that this was the intended indentation:
void decrypt(char lc[], char key[], char dcc[]){    int ioc;    for (int k = 0; k < str.length(lc); k++)    {    if(lc[k] == ' ')        dcc= ' ';    else        ioc = search(key, 26, lc[k])        dcc[k] = char(int('a') + ioc);        dcc[strlength(lc)] = '\o';    }}

Note that the three statements under the 'else' need to have curly brackets around them, or the compiler will only execute the first conditionally and the other two unconditionally.

And the NUL character is '\0', not '\o'.
[edit]

ninja'd += 2;

My OP was messed up and wrong, so normally I'd be correcting it right now but I think the ninjas took care of it
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Quote:Original post by Driv3MeFar
Quote:Original post by dodger_404
void decrypt(char lc[], char key[], char dcc[]){    int ioc;    for (int k = 0; k < str.length(lc); k++)    {...


You don't have a str object here. Are you trying to use a std::string? Or the C std library function strlen?

Also, we can't really do your homework for you here. It's fine to ask questions about it, but make them specific questions that show us you've put some effort into writing and debugging your current work. For example, what have you done so far for this assignment? Did you give up when trying to compile it? Is it running but giving unexpected output? If so, what have you done to debug why the output is incorrect?


i understand that you may not help with homework and i may have worded it wrong when i asked the question, yes i have tried to debug the program. and i guess my specific question would be if you guys could give me hints on how to do the decrypt, i understand look at the line/string read from the messgae and find the corosponding character in the string compared to the key and set that character to a and repeat the process? right wrong?
Is it a substitution cipher then?

The hints we have given so far are to get the code to compile. Once the code compiles, you can start to run it and see if the output matches what you expect.

If you want additional help, you will have to post some revised code indicating that you have taken some steps by yourself.
Quote:Original post by rip-off
Is it a substitution cipher then?

The hints we have given so far are to get the code to compile. Once the code compiles, you can start to run it and see if the output matches what you expect.

If you want additional help, you will have to post some revised code indicating that you have taken some steps by yourself.


ok i can get it to compile but no display?

this is what i have so far


#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;

const int MAX = 26;


istream & fill_array(istream & is, char a[],int MAX, int numUsed);
void decrypt(char lc[], char key[], char dcc[]);
int search(char* key, int len, char val);

int main( )
{
cout << "Lab8 \t\t\t Robert Lury \n\n";
char key[MAX], lc[151], dcc[151];
int number_used;

ifstream fin;
fin.open("key.txt");
if (fin.fail())
{
cerr << "Error opening the input file" << endl;
exit(1);
}
fill_array(fin, key, MAX, number_used);
fin.close();

ifstream fin2;
fin2.open("encrypt.txt");
if (fin2.fail())
{
cerr << "Error opening the input file" << endl;
exit(1);
}

while (fin2.getline(lc,151))
{
decrypt(lc, key, dcc);
cout << dcc << endl;
}
fin2.close();
system("pause");
return(0);
}

istream & fill_array(istream & is, char a[],int MAX, int numUsed)
{
double next;
int i = 0;
for(int i = 0; i < MAX; i++)
is >> a;
return is;
}

int search(char* key, int len, char val)
{
for(int x = 0; x<len; x++)
if (key[x]==val) return x;
return -1;
}

void decrypt(char lc[], char key[], char dcc[])
{
int ioc;
int k = 0;

while(lc[k]!='\0')
{
if(lc[k] == ' ')
dcc[k] = ' ';
else
ioc = search(key, 26, lc[k]);
dcc[k] = char(int('a') + ioc);
k++;
}
dcc[k] = '\0';
}
[code/]
It's [/code], not [code/]. And you should use [source][/source] tags instead for that much code, anyway.

Your problem is here:

while(lc[k]!='\0'){  if(lc[k] == ' ')    dcc[k] = ' ';  else    ioc = search(key, 26, lc[k]);  dcc[k] = char(int('a') + ioc);  k++;}


Notice how I have indented the code. C++ does not care about whitespace, but this indenting shows what really happens. Because there are no braces, the second assignment to dcc[k] happens whether lc[k] == ' ' or not. In the case where it is, the first assignment thus gets wiped out by the second. You only want that assignment to happen when lc[k] is not a space, so you need braces around the 'else'.

Rule of thumb: Always put braces around 'if', 'else' and 'else if' blocks. (Note: the C++ grammar doesn't actually make any special provision for 'else if'. The fact that you can write 'else if' is just a consequence of the fact that C++ doesn't care about whitespace combined with the fact that it lets you put a single statement into an 'if' or an 'else' without braces, combined with the fact that an 'if' block is considered a statement for these purposes. :) )

[Edited by - Zahlman on April 22, 2009 1:27:44 AM]

This topic is closed to new replies.

Advertisement