Can't find where errors are

Where are the errors? It says [Error]a function-definition is not allowed here before "{" token = line 41
[Error]expected '}' at end of input = line 130

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
125
126
127
128
129
130
131
132
133
  #include <iostream>
#include <cstring>
#include <fstream> // read and write from or to files || 	ofstream = write on files		ifstream = read from files
using namespace std;

void encrypt();
void decrypt();
void savefunction();
char save;
int choice; 


int main(){
	
	do {
		
		cout<<"\n\n*****ENIGMA*****\n";
		cout<<"[1] Encrypt \n[2] Decrypt \n[3] Exit \n"; 
		cout<<"Enter choice: ";
		cin>>choice;
		
		switch (choice){
			
			case 1: 		//encryption
				encrypt();
				break;
			case 2: 		//decryption
				decrypt();
				break;
			case 3: 		//exit
				break;
		}
		
	} while (choice!=3);
	
	return 0;
	system("pause");
}

void encrypt(){
	string encrypt(string plaintext,string key){
	string encrypted = "";//wala pay sulod na result
	for(int x = 0, o = 0; x < plaintext.length() ; x++){ //mag loop cya kung unsa katas.a ang word pakapin pod ang key 
		if(plaintext[x]==' '){ // pag space ang makita
			x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
			encrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud 
		}
		int word_pos = get_location(plaintext[x]); //charot kuhaon nya nag posistion sa letter "word[x]" 
		int key_pos = get_location(key[o]); // kuhaon pud nya ang position sa key kung pila ang idungag
		encrypted += get_letter(word_pos + key_pos) ; //kani naline kay kuha.on nya ang letter tapos i sumpay sa encrypted na variable
		/* then is sumpay lang to nya kuntahay kay 'b' ang letter then ang key kay 'c' 
		since ang position sa 'b' kay 1 then ang posistion sa 'c' kay 2 (Note dli na i count gkan pag 'a' so mag start ang 1 sa b)
		then i-add lang nimo para ma encrypt cya 1 + 2 = 3 since ang 3 kay 'd' mao lang to .
		*/
		o++;//kani next letter sa key napod daw ang basahun nya
		if(o > key.length()-1)//pag manubra na kuntahay ang length sa love kay 4 man d back to zero napod (note  since ge array man pag consider ang string d minus 1 ta aron d mulapas) 
			o = 0;//reset nya ang o parabakik tag sugod
	}
	return encrypted;//tapos na done bitches i return nato nya ng ecnryoted na word
}
	
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	
	char yes;string temp;//char yes para sa yes or no then kanang string temp temporary lang pang hold sa first word
			cout<<"\n\n*****ENCRYPTION*****\n\n";
			cout<<"Enter plain text to encrypt: ";
			cin>>temp;//pang hold lang ni
			getline(cin, plaintext);
			//cin.ignore();
			plaintext += temp;//dili na ka ka type
			cout<<"Enter encryption key: ";	   
			getline(cin, key);
			//cin.ignore();	
			cout<<"Encrypted cipher text: "<<encrypt(plaintext,key)<<endl; //call nya ang function with the parameters na
			cout<<"Save cipher text to file? (y/n): ";
			cin>>save;
			savefunction();	
}


void decrypt(){
	string decrypt(string plaintext,string key){
	string decrypted = "";//parehas ra didtos pikas
	for(int x = 0, o = 0; x < plaintext.length() ; x++){//parehas ra didtos pikas
		/*if(plaintext[x]==' '){ // pag space ang makita
			x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
			decrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud 
		}*/
		int word_pos = get_location(plaintext[x]);//parehas ra didtos pikas	
		int key_pos = get_location(key[o]);//parehas ra didtos pikas
		decrypted += get_letter(word_pos - key_pos+26);//parehas ra didto sa pikas plus 26 lang para sa negative vaules minus lang para atras ba balikan ang dapat balikan
		o++;//kani next letter sa key napod daw ang basahun nya
		if(o > key.length()-1)//parehas ra didtos pikas
			o = 0;//parehas ra didtos pikas
		
	}
	return decrypted;//parehas ra didtos pikas
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	string temp;//temp string
			cout<<"\n\n*****DECRYPTION*****\n\n";
			cout<<"Enter plain text to decrypt: ";//same ra sa taas
			cin>>temp;//pang hold lang ni
			getline(cin, plaintext);//kay pag geline
			//cin.ignore();
			plaintext += temp;//dili na ka ka type
			cout<<"Enter decryption key: "; 	   
			getline(cin,key);//same ra sa taas	
			//cin.ignore();
			cout<<"Encrypted cipher text: "<<decrypt(plaintext,key)<<endl;//call nya ang function
}
	
	
void savefunction(){
	if (save=='Y' || save=='y'){
		ofstream file;
		string filename;
	
		cout<<"Enter file name: "; 
		getline(cin, filename); // get user file name 
		filename += ".txt"; // add .txt after file name 
		file.open(filename.c_str(), std::ios_base::app);
		// file<< --> place the cypher text here to the text file 
		file.close();
		cout<<filename<<" saved successfully!";
	}
	else if (save=='N' || save=='n'){
		cout<<"Cipher text not saved. \n";
	}
}	
	
	
	
At line 40 is the beginning of a function,
void encrypt(){

Then at line 41 is the beginning of another, different function,
string encrypt(string plaintext,string key){

That isn't valid, you're trying to nest one function definition inside another.

Did you really intent to have two different functions, both named encrypt? That would be possible, but they must be kept separate.
Can you help me to arrange properly those codes?
Did you really intend to have two different functions, both named encrypt?

I don't know if this is what you wanted or not.
Here's one function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void encrypt(){ 
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX    
    
    char yes;string temp;//char yes para sa yes or no then kanang string temp temporary lang pang hold sa first word
	cout<<"\n\n*****ENCRYPTION*****\n\n";
	cout<<"Enter plain text to encrypt: ";
	cin>>temp;//pang hold lang ni
	getline(cin, plaintext);
	//cin.ignore();
	plaintext += temp;//dili na ka ka type
	cout<<"Enter encryption key: ";       
	getline(cin, key);
	//cin.ignore();    
	cout<<"Encrypted cipher text: "<<encrypt(plaintext,key)<<endl; //call nya ang function with the parameters na
	cout<<"Save cipher text to file? (y/n): ";
	cin>>save;
	savefunction();    
}


And here is the other:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string encrypt(string plaintext,string key){
    string encrypted = "";//wala pay sulod na result
    for(int x = 0, o = 0; x < plaintext.length() ; x++){ //mag loop cya kung unsa katas.a ang word pakapin pod ang key 
        if(plaintext[x]==' '){ // pag space ang makita
            x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
            encrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud 
        }
        int word_pos = get_location(plaintext[x]); //charot kuhaon nya nag posistion sa letter "word[x]" 
        int key_pos = get_location(key[o]); // kuhaon pud nya ang position sa key kung pila ang idungag
        encrypted += get_letter(word_pos + key_pos) ; //kani naline kay kuha.on nya ang letter tapos i sumpay sa encrypted na variable
        /* then is sumpay lang to nya kuntahay kay 'b' ang letter then ang key kay 'c' 
        since ang position sa 'b' kay 1 then ang posistion sa 'c' kay 2 (Note dli na i count gkan pag 'a' so mag start ang 1 sa b)
        then i-add lang nimo para ma encrypt cya 1 + 2 = 3 since ang 3 kay 'd' mao lang to .
        */
        o++;//kani next letter sa key napod daw ang basahun nya
        if(o > key.length()-1)//pag manubra na kuntahay ang length sa love kay 4 man d back to zero napod (note  since ge array man pag consider ang string d minus 1 ta aron d mulapas) 
            o = 0;//reset nya ang o parabakik tag sugod
    }
    return encrypted;//tapos na done bitches i return nato nya ng ecnryoted na word
}


I don't know whether that code makes sense. But at least the two functions are now separated.

You need to do the same with the two decrypt() functions.
Chervil I did what you just told me but there is another problem, an error that says expected constructor, destructor, or type conversion before '(' token.

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <cstring>
#include <fstream>
 // read and write from or to files || 	ofstream = write on files		ifstream = read from files
using namespace std;

void encrypt();
void decrypt();
void savefunction();
char save;
string plaintext, key, encrypt2(string plaintext,string key), decrypt2(string plaintext,string key);;
int choice, word_pos, key_pos; 



int main(){
	
	do {
		
		cout<<"\n\n*****ENIGMA*****\n";
		cout<<"[1] Encrypt \n[2] Decrypt \n[3] Exit \n"; 
		cout<<"Enter choice: ";
		cin>>choice;
		
		switch (choice){
			
			case 1: 		//encryption
				encrypt();
				break;
			case 2: 		//decryption
				decrypt();
				break;
			case 3: 		//exit
				break;
		}
		
	} while (choice!=3);
	
	return 0;
	system("pause");
}

encrypt2 (plaintext, key){
	string encrypted = "";//wala pay sulod na result
	for(int x = 0, o = 0; x < plaintext.length() ; x++){ //mag loop cya kung unsa katas.a ang word pakapin pod ang key 
		if(plaintext[x]==' '){ // pag space ang makita
			x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
			encrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud 
		}
		word_pos = get_location(plaintext[x]); //charot kuhaon nya nag posistion sa letter "word[x]" 
		key_pos = get_location(key[o]); // kuhaon pud nya ang position sa key kung pila ang idungag
		encrypted += get_letter(word_pos + key_pos) ; //kani naline kay kuha.on nya ang letter tapos i sumpay sa encrypted na variable
		/* then is sumpay lang to nya kuntahay kay 'b' ang letter then ang key kay 'c' 
		since ang position sa 'b' kay 1 then ang posistion sa 'c' kay 2 (Note dli na i count gkan pag 'a' so mag start ang 1 sa b)
		then i-add lang nimo para ma encrypt cya 1 + 2 = 3 since ang 3 kay 'd' mao lang to .
		*/
		o++;//kani next letter sa key napod daw ang basahun nya
		if(o > key.length()-1)//pag manubra na kuntahay ang length sa love kay 4 man d back to zero napod (note  since ge array man pag consider ang string d minus 1 ta aron d mulapas) 
			o = 0;//reset nya ang o parabakik tag sugod
	}
	return encrypted;//tapos na done bitches i return nato nya ng ecnryoted na word
}


void encrypt(){
	
	
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	
	char yes;string temp;//char yes para sa yes or no then kanang string temp temporary lang pang hold sa first word
			cout<<"\n\n*****ENCRYPTION*****\n\n";
			cout<<"Enter plain text to encrypt: ";
			cin>>temp;//pang hold lang ni
			getline(cin, plaintext);
			//cin.ignore();
			plaintext += temp;//dili na ka ka type
			cout<<"Enter encryption key: ";	   
			getline(cin, key);
			//cin.ignore();	
			encrypt2(plaintext, key);
			cout<<"Encrypted cipher text: "<<encrypt2(plaintext,key)<<endl; //call nya ang function with the parameters na
			cout<<"Save cipher text to file? (y/n): ";
			cin>>save;
			savefunction();	
}

decrypt2(plaintext,key){
	string decrypted = "";//parehas ra didtos pikas
	for(int x = 0, o = 0; x < plaintext.length() ; x++){//parehas ra didtos pikas
		/*if(plaintext[x]==' '){ // pag space ang makita
			x++;//x++ mag skip og read sa letter kay space man unsa.on man nato kung space paasa kaau ning space
			decrypted += " ";//pag space as usual butangan niya og space e aron mag bulag cla space lang jud 
		}*/
		word_pos = get_location(plaintext[x]);//parehas ra didtos pikas	
		key_pos = get_location(key[o]);//parehas ra didtos pikas
		decrypted += get_letter(word_pos - key_pos+26);//parehas ra didto sa pikas plus 26 lang para sa negative vaules minus lang para atras ba balikan ang dapat balikan
		o++;//kani next letter sa key napod daw ang basahun nya
		if(o > key.length()-1)//parehas ra didtos pikas
			o = 0;//parehas ra didtos pikas
		
	}
	return decrypted;//parehas ra didtos pikas
}	

void decrypt(){
	
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX	
	string temp;//temp string
			cout<<"\n\n*****DECRYPTION*****\n\n";
			cout<<"Enter plain text to decrypt: ";//same ra sa taas
			cin>>temp;//pang hold lang ni
			getline(cin, plaintext);//kay pag geline
			//cin.ignore();
			plaintext += temp;//dili na ka ka type
			cout<<"Enter decryption key: "; 	   
			getline(cin,key);//same ra sa taas	
			//cin.ignore();
			string decrypt2(plaintext,key);
			cout<<"Encrypted cipher text: "<<decrypt2(plaintext,key)<<endl;//call nya ang function
}

	
void savefunction(){
	if (save=='Y' || save=='y'){
		ofstream file;
		string filename;
	
		cout<<"Enter file name: "; 
		getline(cin, filename); // get user file name 
		filename += ".txt"; // add .txt after file name 
		file.open(filename.c_str(), std::ios_base::app);
		// file<< --> place the cypher text here to the text file 
		file.close();
		cout<<filename<<" saved successfully!";
	}
	else if (save=='N' || save=='n'){
		cout<<"Cipher text not saved. \n";
	}
}	
	
	
	
The function header at line 43:
 
encrypt2 (plaintext, key){
should look like this
 
string encrypt2(string plaintext,string key) {

Lines 59-61 functions get_location() and get_letter() don't exist.

at line 87
 
decrypt2(plaintext,key){
should be
 
string decrypt2(string plaintext,string key){


Line 118 string decrypt2(plaintext,key); is an invalid function declaration, remove it.
Topic archived. No new replies allowed.