URGENT HELP

Hello.
I am a beginner and I have this code that has been killing me.
Any and all help is appreciated.
Its a cipher and for some reason it won't work.
It opens and asks for user input but doesn't execute anything correctly.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{	char a;
	char cipher (char);
	string input;
	string filename;
	string ofilename;
	string line;
	ifstream infile;
	ofstream outfile;

	cout<<"Enter 'e' to encrypt/Enter 'd' to encrypt: ";
	cin>>a;
	
	if (a=='e'||a=='E') {
			
			
			cout << "Input filename: ";
			cin>>filename;
			infile.open(filename.c_str());

			if (!infile.is_open()) {
				cout<<"file cannot be opened"<<endl;
				return 0;
			}				
				
			cout<< "Output filename: ";
			cin>>filename;
			outfile.open(filename.c_str());
		
					
			do {
			getline (cin,line);
			string output = "";
			for(int x = 0; x < input.length(); x++) {
			
            output += cipher(input[x]);
			}
			cout<<output<<endl;
			outfile<<output;
			}while (!input.length() == 0);
		}

	else if (a=='d'||a=='D'); {
			
				
			cout << "Input filename: ";
			cin>>filename;
			infile.open(filename.c_str());

			if (!infile.is_open()) {
				cout<<"file cannot be opened"<<endl;
				return 1;
			}				
			
				
			cout<< "Output filename: ";
			cin>>filename;
			outfile.open(filename.c_str());
		
			
			do {
			getline (cin,line);
			string output = "";
			for(int x = 0; x < input.length(); x++) {
			
            output += cipher(input[x]);
			}
			cout<<output<<endl;
			outfile<<output;
			}while (!input.length() == 0);
		}
	}

char cipher (char c) {

			if (isalpha (c))	
	{		c = toupper(c);
			c = (((c-65)-5) % 26) + 65;
	}
			system ("pause");
			return c;
}

closed account (Dy7SLyTq)
line 7
why do you have char a(char) instead of char a?
because of the use of

output += cipher(input[x])
It took me a minute to realize, but line 7, char cipher (char);, is the function prototype for the cipher function. You should probably put that before the main function. It works the way it is, but like I said, it's a bit confusing (at least it was to me) to try and read it.

It opens and asks for user input but doesn't execute anything correctly.

Can you elaborate on this a little bit (what exactly happens?).

I think you may be having issues with mixing std::cin and std::getline (maybe one of the more experienced members can explain why that is because I'm not confident enough with my understanding to even try explaining it). For now, try adding cin.get() after each cin>> statement, and see if that helps.
Function declarations are usually done outside of main for clarity, not to mention if declared inside main you can only use it there.

at line 36 you have:
getline (cin,line);
I assume you want to read infile into input so try
getline (infile, input);
same problem appears on line 66.

also don't forget to close infile and outfile while you're done with them using the close() function
Last edited on
Thanks!!
I got it to read and write.
This is the revised 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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{	char a;
	char cipher (char);
	string input;
	string filename;
	string ofilename;
	string line;
	ifstream infile;
	ofstream outfile;

	cout<<"Enter 'e' to encrypt/Enter 'd' to encrypt: ";
	cin>>a;
	
	if (a=='e'||a=='E') {
			
			
			cout << "Input filename: ";
			cin>>filename;
			infile.open(filename.c_str());

			if (!infile.is_open()) {
				cout<<"file cannot be opened"<<endl;
				return 0;
			}				
				
			cout<< "Output filename: ";
			cin>>filename;
			outfile.open(filename.c_str());
		
					
			do {
			cin.ignore();
			getline (infile,input);
			string output = "";
			for(int x = 0; x < input.length(); x++) {
			
            output += cipher(input[x]);
			}
			cout<<output<<endl;
			outfile<<output;
			outfile.close();
			system ("pause");
			return a;
			}while (!input.length() == 0);
		}

	else if (a=='d'||a=='D'); {
			
				
			cout << "Input filename: ";
			cin>>filename;
			infile.open(filename.c_str());

			if (!infile.is_open()) {
				cout<<"file cannot be opened"<<endl;
				return 1;
			}				
			
				
			cout<< "Output filename: ";
			cin>>ofilename;
			outfile.open(ofilename.c_str());
		
			
			do {
			cin.ignore();
			getline(infile,input);
			string output = "";
			for(int x = 0; x < input.length(); x++) {
			
            output += cipher(input[x]);
			}
			cout<<output<<endl;
			outfile<<output;
			outfile.close();
			system ("pause");
			return a;
			}while (!input.length() == 0);
		}
	}

char cipher (char c) {

			if (isalpha (c))	
	{		c = toupper(c);
			c = (((c-65)+5) % 26) + 65;
	}
			
			return c;
}

only problem now is the fact that it only reads and writes the first line..
Any suggestions?
Firstly, you should only close the files once you're done with them, so after the loop.
Secondly, you're returning in the loop, return will just simply exit the function regardless of where you are (in this case it will exit main, closing the program).

Finally, a more common while condition while reading a file is
while(!infile.eof()) // continue while you have not reached the end of the file
closed account (Dy7SLyTq)
personally i like while(getline) better
Warnis wrote:
Finally, a more common while condition while reading a file is
while(!infile.eof())

It may be common on the Internet, but it's wrong.
It's wrong isn't exactly the most helpful statement, I've been away for a while so I've certainly forgotten parts. What would you suggest being "right" then?
while(infile.is_open())
and checking for eof and closing in the loop?
while(infile.getline(x,y)
or as suggested above by DTSCode?
Or something else?
while(getline(infile, x)) is indeed what's usually done in C++ to process a file line by line.
Topic archived. No new replies allowed.