C++ Encryption/Decryption problem

Aug 2, 2009 at 5:12pm
Hello,

I am trying to write a program that gives you 3 options:

1 for encryption
2 for decryption
3 Exits the program

If option 1, my program will ask for a password, an input file name and an output file name. My program will then read the file and mesh it with the password resulting in an encrypted file. It will write this file out.

If option 2, my program will ask for the password and the encrypted file name. Read the file and perform a decryption, printing the decryption on the screen

If option 3, the program will stop.



My problem is that my program is giving me 3 errors:
1)syntax error : 'return'
2)'encrypt' : function does not take 2 arguments
3)'decrypt' : function does not take 2 arguments

Coding:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>


using namespace std;

int menu();
void decrypt();
void encrypt();



void main(){
int choice = 4;
srand((unsigned)time(0)); 

while ((choice = menu()) != 3){
cout << "Menu returned: " << choice << endl;
if (choice == 1) encrypt();
else if (choice == 2) decrypt();
else if (choice += 3) cout << "Bad choice: " << choice << endl;
}

return;
}


int menu()
{
int choice;
cout<<"Please pick one of the following:.\n";
cout<<"1.Encryption.\n";
cout<<"2.Decryption.\n";
cout<<"3.Close program.\n";

cin>>choice;

return choice;

}

void encrypt(){
ifstream in_stream;
ofstream out_stream;
int k=0;
int ranNum;
ranNum =rand()%125;
ranNum=ranNum+1;

cout<<"Opening proj3cpp.txt for encryption.\n";

in_stream.open("proj3cpp.txt");
out_stream.open("ciphertext_out.txt");

while(!in_stream.eof())
{
out_stream<<ranNum;
if(k>=126)
out_stream<<'a';

else
out_stream<<
return;
}

out_stream.put(encrypt("projcpp.txt","ranNum"));
if(in_stream.fail())
{
cout<<"Input file opening has failed.\n";
exit(1);
}
out_stream.open("ciphertext_out.txt");
if(out_stream.fail())

cout<<"Output file opening has failed.\n";
exit(1);
return;
}


void decrypt()
{
int k=0;
int ranNum;
ranNum =rand()%125;
ranNum=ranNum+1;
ifstream in_stream;
ofstream out_stream;

cout<<"Opening file to begin decryption.\n";

in_stream.open("ciphertext_out.txt");
out_stream.open("crypt.txt");
while(!in_stream.eof()){
if(k<=126)
out_stream<<k;
else
out_stream<<k;
out_stream<<ranNum;
out_stream.put(decrypt("ciphertext_out.txt",k));

if(in_stream.fail())
{
cout<<"Input file has failed to open.\n";
exit(1);}
return;
}


}

I can't seem to find out what the problem is.

Thanks in advance.
Last edited on Aug 4, 2009 at 8:01pm
Aug 2, 2009 at 5:51pm
I gave your code a one-over. I might have missed some stuff, but this is the stuff I noticed. Also... get in the habit of indenting properly. Indentation makes code much easier to read.

line 1:
#include "stdafx.h"
This isn't really an error, but MSVS likes to put in this stdafx crap by default. Break the habit. When you start a new project, check the "empty project" box, then you don't need the stdafx nonsense, and other people will be able to more easily compile your code.

line 15:
void main(){
main should always return an int. Not a void. You'll also have to change the return on line 26 to return 0;

line 23:
else if (choice += 3)
I think you meant >=... not +=

line 64:
out_stream<<
What are you trying to do here? You can't leave the << hanging like this. I'd remove the entire else block (delete lines 63,64)

line 68:
out_stream.put(encrypt("projcpp.txt","ranNum"));
You're trying to call 'encrypt' with two strings, but encrypt does not take two strings -- it doesn't take any parameters at all. It also doesn't return anything, so you can't put it inside of a put() call like this either.

Unless you have another function called encrypt somewhere -- but it's not visible to this program.


line 102:
out_stream.put(decrypt("ciphertext_out.txt",k));
Same problem. decrypt doesn't take any parameters, but you're giving it two. It also doesn't return anything, so you can't put it in put() like this.
Aug 2, 2009 at 6:16pm
main should always return an int. Not a void. You'll also have to change the return on line 26 to return 0;

As Irwin of CEF states, "ISO compliance has no direct implication for how main should be handled in regards to declaration/returning." A void function has no defined return value, therefore it is simply the last usage of EAX. Given this information, there's nothing wrong with using void main(), simply that almost all programmers are taught to use "int main()" from their first hello world program.
Last edited on Aug 2, 2009 at 6:17pm
Aug 2, 2009 at 6:43pm
there's nothing wrong with using void main()


All C/C++ compilers everywhere will accept int main() as a valid entry point. The same cannot be said of void main().

Why risk compatibility for something so insignificant?


EDIT: rephrased to sound less like an asshole.

EDIT2: random relevent links:

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143

http://users.aber.ac.uk/auj/voidmain.shtml
Last edited on Aug 2, 2009 at 6:51pm
Aug 2, 2009 at 6:55pm
Thank you Disch and Kiana for your kindness and your replies.

Disch, sorry about the bad indentation, it was lost from copying and pasting so much to different things.

I fixed the .puts and deleted lines 63 and 64.

I've read what you wrote about the two strings and I'm ashamed to say that I'm still not quite sure how to fix the problem, what do I need to change?
Last edited on Aug 2, 2009 at 7:02pm
Aug 2, 2009 at 7:15pm
what do I need to change?


Well what are you trying to do?

This is your code:
encrypt("projcpp.txt","ranNum");

This attempts to call a function called encrypt -- but such a function doesn't exist! What are you trying to accomplish with this code? Is there another function called encrypt that you forgot to write?
Aug 4, 2009 at 7:29pm
I have changed my coding completely and running into a few problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

void do_again(),direction(int ans);
void option_1(),option_2(),option_3();

int main()

{
     do_again();//calls do_again function

     return 0;
}//end function

void do_again()
{
     int ans;//this will be used every time
     do
     {   //User menu
          cout << "Hello and welcome.\n\n";
          cout << "Please choose one of the following.\n";
          cout << "1: Encryption\n";
          cout << "2: Decryption\n";
          cout << "3: Exit\n";
               cin  >> ans;
               cin.ignore(80,'\n'); //this reads what the user chose
          direction(ans);//sends the answer to direction
     }while(ans!=3);//program keeps going until the user chooses exit
}//end of this function

void direction(int ans)
{
     switch (ans)//Go through the options
     {
          case 1:
               option_1();
               break;
          case 2:
               option_2();
               break;
          case 3:
               option_3();
               break;
          default://If anything other than 1,2,3 is chosen
               cout << "The option you have chosen was not listed, please try again.\n";
               break;
     }
}//end of case functions


void option_1() //If option 1 is choosen, encryption begins
{
char a[5000];
char pw[15];
char buffer[256];
int x,y;
int i,j;
int choice=4;

ifstream examplefile ("c:\\proj3cpp.txt");
 
  if (! examplefile.is_open())
  {
     cout << "Error opening file";
     exit (1);
  }

  while (! examplefile.eof() )
  {
    examplefile.getline (buffer,100);
    cout << buffer << endl;
  }                    
           
cout<<endl<<"Please enter a password: "; //User inputs a password
cin>>pw;

x=strlen(a);
y=strlen(pw);
j=0;
for (i=0; i<x; i++)
    {
        if (j>=y) j=0;
        a[i]=a[i]+pw[j];
    }
cout<<endl<<a;
cout<<endl<<"Encryption is now complete and has been saved to a text document.\n\n";
}

void option_2()// If option 2 is chosen, decryption begins
{
}

void option_3()// If option 3 is chosen, program closes.
{
}


My problem is how do I get it to actually encrypt the text file I have and save the encrypted text to a new text file?

Also, it needs to make the text file =a so it goes through the encryption process, is there a way to do that?
Topic archived. No new replies allowed.