Having trouble with encoding program

I'm trying to use an array to encode a line from a file and then output the encoded line to a separate file. The program should look to the array to add a number the characters on the ASCII table. So the first element in the array changes the first character in the line from the file and the second one changes the second one and so on through five and then it should start over with the first array element if it is more than the array.

Any help would be much 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
int main()
{

  // data
  int i; // loop counter (int)
  string fileName; // name of file input by the user (string)
  string fileLine; // line from the file to encode (string)
  ifstream fin; // file input (ifstream)
  ofstream fout; // file output (ofstream)
  const int SIZE = 5;
  int offset[SIZE] = {-5, 8, 12, 6, 1};
  int counter = 0;
  int index;
  char keepGoing;

  // prompt user for fileName
  cout << "Enter the name of input file you wish to encode [i.e. encode.txt]: ";
  getline(cin, fileName);
  cout << endl;
  
  // open the file for input
  fin.open(fileName.c_str()); 
  if (!fin.good()) throw "I/O error"; 

  // open secret.txt for output
  fout.open("secret1.txt");
  if (!fout.good()) throw "I/O error"; 

  // convert each character in line by adding and subtracting with the elements in the array
  while (fin.good())
  {
    getline(fin, fileLine);
    cout << "Line from file: " << fileLine << endl;
    while (true)
    {
      // cycle through array
      int index = counter % SIZE;
      cout << offset[index] << endl;

      // continue cycling?
      cout << "Do you want to continue? [Y/N]: ";
      cin >> keepGoing;
      cin.ignore(1000, 10);
      if (keepGoing == 'n' || keepGoing == 'N') break;
    
      for (i = 0; i < fileLine.length(); i++)
      {
        fileLine[i] = fileLine[i] + offset[i];      
      }// for converting
      // count loops
      counter++;
    }// while true

    // ouput result to secret1.txt and to console
    fout << fileLine << endl;
    cout << "Encoded form: " << fileLine << endl << endl;

  }// while loop

  // clear and close files
  fin.clear();
  fout.clear();
  fin.close();
  fout.close();

} // main  
Topic archived. No new replies allowed.