Homework Help

I'm doing an assignment for my C++ class where I am encrypting data from a file using the Caesar Cipher shift method where the character is shifted three places down the alphabet (without looping back around in the case of z). I'm taking that encryption, or decryption, and printing it to stdout and to a file called message.

I'm having issues with printing the decrypted message to stdout. For whatever reason, for messages of multiple lines, my code only displays the last decoded line to stdout, but the file I print the decoded message to has a full decoded message. Help??

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
#include <iostream>
#include <fstream>          // ofstream and ifstream variables
#include <string>           // user input string filename
#include <cstring>          // c_str()

using namespace std;

int main()

{

  //Data Abstraction:

    ofstream message;       // To write results to file
    ifstream file;          // To read file
    string   filename;      // User enters filename convert to string

    int      attempts = 0,  // For file validation loop
             choice;        // For switch statement
    char     letter;        // For reading file

  //Input and Process:

  do
  {

  cout << "Enter file name: ";
  cin  >> filename;
  file.open(filename.c_str());
  attempts++;

    if (!(file.is_open()))
    cout << "File could not be found, try again..." << endl;
  }

  while (!(file.is_open()) && (attempts < 3));

  //Process/Output:

  if (file.is_open())
  {

  message.open("message.txt");
  cout << "[1]Encrypt" << endl;
  cout << "[2]Decrypt" << endl;
  cin  >> choice;

    switch (choice)
    {

    case (1) : while (file >> noskipws >> letter)
                {
                  letter += 3;
                  message << letter;
                  cout    << letter;
                }

    break;

    case (2) : while (file >> noskipws >> letter)
              {
                letter -= 3;
                message << letter;
                cout    << letter;
              }

    break;

    default: cout << "Invalid input, restart and try again" << endl;

    }

  }

  file.close();
  message.close();

  return 0;

}

Last edited on
Your program is working fine. Make sure you don't try to encrypt/decrypt "message.txt" -- otherwise you get read/write collisions.


C:\prog\caesar> copy con plain.txt
Hello world!
Time flies like an arrow,
Fruit flies like a banana.
^Z
        1 file(s) copied.

C:\prog\caesar> a
Enter file name: plain.txt
[1]Encrypt
[2]Decrypt
1
Iuxlw#iolhv#olnh#d#edqdqd1
C:\prog\caesar> dir/b
a.cpp
a.exe
message.txt
plain.txt

C:\prog\caesar> copy message.txt cipher.txt
        1 file(s) copied.

C:\prog\caesar> a
Enter file name: cipher.txt
[1]Encrypt
[2]Decrypt
2
Hello world!
Time flies like an arrow,
Fruit flies like a banana.

C:\prog\caesar> fc message.txt plain.txt
Comparing files message.txt and PLAIN.TXT
FC: no differences encountered

C:\prog\caesar>

:O)
Off topic and not a big deal, but this caught my eye:

12
13
 //Data Abstraction:


This is actually just the opposite of abstraction. Abstraction is when you deal with concepts without worrying about the details. Here you are making the details concrete by defining the variables, so there is no abstraction here.

A better comment would be something like
12
13
  //Data Definitions:
Topic archived. No new replies allowed.