Very Simple Encode/Decode program, need help

Hi, I'm new to C++ and need some help involving encoding and decoding text from text files. When I enter a file in the program just stalls. Any assistance would be appreciated!

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
  

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

    char getmenuselection (char m);
    void encrypt (char key[]);
    void decrypt (char code[]);


    int main()
    {
    	ifstream input;
    	ifstream in;
    	ofstream out;
    	char menu;
    	char file [80];
    	char codekey;
    	char myname[128];
    	menu=0;

    	menu=getmenuselection(menu); //Asks user what function to use.

    		if (menu == 'e' || menu == 'E') //Option to encrypt a file.
    		{
    		cout << "Enter original file name... ";
    		input.open (file);
    		cin >> file;
    		encrypt (file);
    									
    		return main();
    		}
    		if (menu == 'd'|| menu == 'D')//Option to decrypt a file.
    		{
    		cout << "Enter original file name... ";
    		input.open (file);
    		cin >> file;
    		decrypt (file);
    		return main();
    		}
    		if (menu == 'q') //Option to end the program.
    		{
    		cout << "Goodbye Agent";
    		cin.ignore(2);
    		return 0;
    		}
    cin.ignore(2);
    return 0;				
    }
    char getmenuselection (char m) //Menu selection function.
    {
    	cout << "Select 'E' to encrypt a file or select 'D' to decrypt a file. ";
    	cin >> m;
        switch (m)
        {
           case 'e':
    	   case 'E':   cout << "You have selected to encrypt a file... " << endl;
                     break;
           case 'd':
    	   case 'D':   cout <<  "You have selected to decrypt a file... " << endl; 
    				break;
    	   case 'q':
    	   case 'Q':   cout <<  "You have selected to quit..." << endl; 
    				break;
           default: cout << "Invalid entry, please try again... ";                
    				return getmenuselection (m);
    }return (m);
    }
    void encrypt (char key[]) //encryption function.
    {
    	ifstream infile;
    	infile.open("key");
		string item;
		while(!infile.eof())
		{
			infile>>item;
		}
		
    	for(int i = 0; i < 5; i++) item[i] += 2; 
    	infile.close();
    }										
    void decrypt (char code[]) //decryption function.
    { 	ifstream outfile;
    	outfile.open("code");
		string item;
		while(!outfile.eof())
		{
			outfile>>item;
		}
		
    	for(int i = 0; i < 5; i++) item[i] -= 2;
outfile.close()
    }
It seems that key in encrypt funtion is strng and also code in descrypt, so don't use double quote when opening file.
As far as I can see, "file" is never initialised. So your program will probably crash in line 31.
In line 32, if what you're trying to do is read from the opened file, you should be doing something like
1
2
string word;
input >> word;
to get the next word from the opened file, just like you do in lines 77 to 81, if that's not clear (my fault), consider commenting out lines 31 and 40, and following the @LendraDwi by using
1
2
infile.open(key);
outfile.open(code);
in lines 76 and 88.
Even better make "file" a string instead of char[80], and use something like encrypt(file.c_str());
line 96 needs a semi-colon.
Also you never your output, so in encrypt() and decrypt(), you need to create an ofstream and dump your contents in it,
your process (encryption/encription) happens after the loop, meaning it will only operate on the last item not on all of them, and unless you only intend to scramble the first 5 characters of each item, you should get rid of the magic number "5" in lines 83 and 95 (what happens if the item is less than 5 characters long?)
For example, encrypt might look like:
1
2
3
4
5
6
7
8
9
10
11
12
  	ifstream infile(key);
                ofstream outfile((string(key) + ".enc").c_str());
		string item;
		while(!infile.eof())
		{
			infile>>item;
                       for(int i = 0; i < item.size(); i++) item[i] += 2; 
                       outfile << item;
		}
		
    	outfile.close();
    	infile.close();

This will give you an output file with extension ".enc" containg your encrypted data.
There are many more changes to be made for robustness of the code, but I hope these few tips will help you understand why it's currently failing.
Topic archived. No new replies allowed.