Printing ifstream

So inside main it asks for a password followed by an if statement that calls a print function:

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
//Open password file
	password.open("E:\\College\\2016 Spring\\C++\\Final\\Password.txt");
	if (!password)
	{
		cerr << "Unable to find password. \n";
		system("pause");
		return 1;
	}

	//Get password
	getline(password, pass);

	//Ask for password
	cout << "Please enter password: ";
	cin >> entry;

	//Confirm password
	if (entry.compare(pass) == 0)
	{
		//Call print function if password is correct
		print(encrypt);
	}

	if (entry != pass)
	{
		cout << "Incorrect.\n";
	}


and this is what the function looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Print decrypted contents
void print(ifstream& infile)
{
	char file;
	vector<char>read;
	int i = 0;

	//Read infile to vector
	while (!infile.eof())
	{
		infile >> file;
		read.push_back(file);
	}

	//Print read vector
	for (i = 0; i < read.size(); i++)
	{
		cout << read[i];
	}
}


Whenever I run it it recognizes an incorrect password but with the correct password it just goes straight to system pause without printing anything. What am I doing wrong?
> but with the correct password it just goes straight to system pause
the only system("pause"); that you have is in line 6 when you can't open the file. There is no such thing in the "correct password" path.
¿what's the point analysing code that doesn't correspond with your symptoms?


Also, you need to learn to simplify your testcase. You think that the problem is with printing, ¿how is the password thing relevant?
Last edited on
I didn't put the entire code. Here's the entire code:

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
113
114
115
116
117
118
119
120
121
122
123
124
//Program will take file contents and encrypt them
//With password program will read decrypted contents

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

using namespace std;

void encryption(ifstream&, ofstream&);
void print(ifstream&);

int main()
{
	ifstream password, encrypt;
	ofstream decrypt;
	string pass, entry;

	//Open File.txt
	encrypt.open("E:\\College\\2016 Spring\\C++\\Final\\File.txt");
	if (!encrypt)
	{
		cerr << "Unable to open file. \n";
		system("pause");
		return 1;
	}

	//Open Encrypt.txt
	decrypt.open("E:\\College\\2016 Spring\\C++\\Final\\Encrypt.txt");
	if (!decrypt)
	{
		cerr << "Unable to open file. \n";
		system("pause");
		return 1;
	}

	//Call encryption function
	encryption(encrypt, decrypt);

	//Open password file
	password.open("E:\\College\\2016 Spring\\C++\\Final\\Password.txt");
	if (!password)
	{
		cerr << "Unable to find password. \n";
		system("pause");
		return 1;
	}

	//Get password
	getline(password, pass);

	//Ask for password
	cout << "Please enter password: ";
	cin >> entry;

	//Confirm password
	if (entry.compare(pass) == 0)
	{
		//Call print function if password is correct
		print(encrypt);
	}

	if (entry != pass)
	{
		cout << "Incorrect.\n";
	}

	password.close();
	decrypt.close();
	encrypt.close();

	system("pause");
	return 0;
}


//Encrypt by adding 8 to each value of vector
void encryption(ifstream& encrypt, ofstream& decrypt)
{
	char text1 = 'A',
		text2 = 'B';
	int i = 0;
	vector<char> out;

	//Read file and put into vector
	while (!encrypt.eof())
	{
		encrypt >> text1;
		text2 = text1 + 8;
		out.push_back(text2);
	}

	//Write encrypted file
	for (i = 0; i < out.size(); i++)
	{
		decrypt << out[i];
	}
}



//Print decrypted contents
void print(ifstream& infile)
{
	char file;
	vector<char>read;
	int i = 0;

	//Read infile to vector
	while (!infile.eof())
	{
		infile >> file;
		read.push_back(file);
	}

	//Print read vector
	for (i = 0; i < read.size(); i++)
	{
		cout << read[i];
	}
}

I'm pretty sure it's going to the system("pause") at the end of main, I just can't figure out why.
encrypt() makes the fstream to reach eof.
then print() starts at eof, so nothing to print.
Oh! Alright, what's the best way to reset it so it reads from the beginning?
Topic archived. No new replies allowed.