Did I do this right?

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
void Encryption()
{
cout << "\nWhat is the location of the file you would like to encrypt?" << endl;
string fileloc;
getline (cin, fileloc); //cin gets the file location as a string
cin >> fileloc;
infile.open (fileloc.c_str()); //opens the file to be encrypted
infile2.close();

cout << "\nWhat is the password you would like to use to create the encryption?" << endl;
cout << "\nPlease enter a four letter word." << endl;
string password;
getline (cin,password); //cin gets the meshing password as a string
cin >> password;

cout << "\n\nWhat is the name of the file to which you would like to print the encryption?" << endl;
string outfileloc;
getline (cin, outfileloc); //cin gets the output file as a string
cin >> outfileloc;
outfile.open (outfileloc.c_str()); //opens the output file

while (!infile.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be encrypted
infile.get(name); //gets character from the file
name = name + password[i]; //adds the part of the password array
name = name - password[0]; //subtracts the first letter of the password (keeps characters printable)
outfile << name; //prints that character to the output file
}
}

infile.close(); //closes the original file
outfile.close(); //closes the file encryption

cout << "\n\n"; //aesthetic purposes
}


void Decryption()
{
cout << "\nWhat is the location of the file you would like to decrypt?" << endl;

string fileloc;
getline (cin, fileloc); //cin gets the location of the file to be decrypted as a string
cin >> fileloc;
infile2.open (fileloc.c_str()); //opens the file using infile2 (different input file)

cout << "\nWhat is the password used when the text was encrypted?" << endl;
cout << "\nPlease enter the four letter word." << endl;
string password;
getline (cin,password); //gets password used for encrypting as a string
cin >> password;
cout << "\n\n";

while (!infile2.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be decrypted
infile2.get(name); //gets character from file

name = name + password[0]; //re-adds the first letter of the password
name = name - password[i]; //subtracts the proper letter from the password array
cout << name; //prints decrypted character to screen
}

}

infile2.close(); //closes file that was being decrypted

cout << "\n\n"; //aesthetic purposes
return 0;
}

That depends on what it's supposed to do.

Also, please use the code format tags to format your code.
its suppose to encrypt a file
It's probably ok for school, it depends on what you've been taught to date.

It's not ok outside of that environment. There are a number of improvements that can be made, but it probably works the way it is.
it didn't work
First, I'll put all of that in code tags and indent it so I can actually read it.
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
void Encryption()
{
  cout << "\nWhat is the location of the file you would like to encrypt?" << endl;
  string fileloc;
  getline (cin, fileloc);	//cin gets the file location as a string
  cin >> fileloc;
  infile.open (fileloc.c_str());	//opens the file to be encrypted
  infile2.close();
  cout << "\nWhat is the password you would like to use to create the encryption?" << endl;
  cout << "\nPlease enter a four letter word." << endl;
  string password;
  getline (cin,password);	//cin gets the meshing password as a string
  cin >> password;
  cout << "\n\nWhat is the name of the file to which you would like to print the encryption?"<< endl;
  string outfileloc;
  getline (cin, outfileloc);	//cin gets the output file as a string
  cin >> outfileloc;
  outfile.open (outfileloc.c_str()); //opens the output file
  while (!infile.eof())	//while not the end of the file
  {
    for (int i=0 ;i<4 ; i++)	//for loop cycles through the positions of the password array until end of file is reached
    {
      char name;	//character from file to be encrypted
      infile.get(name);	//gets character from the file
      name = name + password[i];	//adds the part of the password array
      name = name - password[0];	//subtracts the first letter of the password (keeps characters printable)
      outfile << name;	//prints that character to the output file
    }
  }
  infile.close();	//closes the original file
  outfile.close();	//closes the file encryption
  cout << "\n\n";	//aesthetic purposes
}

void Decryption()
{
  cout << "\nWhat is the location of the file you would like to decrypt?" << endl;
  string fileloc;
  getline (cin, fileloc);	//cin gets the location of the file to be decrypted as a string
  cin >> fileloc;
  infile2.open (fileloc.c_str());	//opens the file using infile2 (different input file)
  cout << "\nWhat is the password used when the text was encrypted?" << endl;
  cout << "\nPlease enter the four letter word." << endl;
  string password;
  getline (cin,password);	//gets password used for encrypting as a string
  cin >> password;
  cout << "\n\n";
  while (!infile2.eof())	//while not the end of the file
  {
    for (int i=0 ;i<4 ; i++)	//for loop cycles through the positions of the password array until end of file is reached
    {
      char name;	//character from file to be decrypted
      infile2.get(name);	//gets character from file
      name = name + password[0];	//re-adds the first letter of the password
      name = name - password[i];	//subtracts the proper letter from the password array
      cout << name;	//prints decrypted character to screen
    }
  }
  infile2.close();	//closes file that was being decrypted
  cout << "\n\n";	//aesthetic purposes
  return 0;
}


2nd: Does your compiler return any errors? It helps to read through them....
infile, outfile, and infile2 aren't defined anywhere
Decryption() is of type void yet it returns 0
Why so you try to get the same input more than once. Once with getline() then once with the extraction operator?
Why do you try to get the same input twice in several places? Once with getline(), once with the extraction operator.

What inputs do you use?
Topic archived. No new replies allowed.