I'm working on a decoding program, and I'm having trouble with a string which I want to give a variable size, which causes me "Segmentation Fault (core dumped)" if I give it a constant size and then "str.resize" it, debugger tells me:
error: request for member ‘resize’ in ‘line’, which is of non-class type. The problem is with line 74.
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string code = "the quick brown fox jumps over the lazy dog";
int x = code.length();
int checkCode(string a[], int size) {
int numcode = 8;
int numspace = 0;
int numret = 0;
for (int i = 0; i < size; i++) {
string b = a[i];
int c = b.length();
if (c == x) {
for (int j = 0; j < c; j++) {
if (b[j] == ' ') numspace++;
}
if (numspace == numcode) {
numret = i;
i = size;
}
}
}
return numret;
}
string getCode(string d) {
string dec = "abcdefghijklmnopqrstuvwxyz";
char loc = ' ';
int q = d.length();
for (int i = 0; i < q; i++) {
if (d[i] != ' ') {
loc = d[i] - 'a';
dec[loc] = code[i];
}
}
return dec;
}
void decode(string s[], int size, string d, string name) {
char loc = ' ';
ofstream arch;
arch.open(name.c_str());
for (int i = 0; i < size; i++) {
string n = s[i];
int t = n.length();
for (int j = 0; j < t; j++) {
if (n[j] != ' ') {
loc = n[j] - 'a';
n[j] = d[loc];
}
}
arch << n << endl;
}
}
int main() {
int num = 0,codeN,cont;
string dec, name;
cout << "Coded file name: ";
cin >> name;
//Open file and creates an array based on size
ifstream arch;
arch.open((name).c_str());
while (getline(arch,dec)) {
num++;
}
string line[30];
line.resize(num);
arch.clear();
arch.seekg(0);
cont = 0;
while (!arch.eof()) {
getline(arch,line[cont]);
cont++;
}
arch.close();
cout << "Decoded file name: ";
cin >> name;
codeN = checkCode(line,num);
dec = getCode(line[codeN]);
decode(line,num,dec,name);
cout << "File ready: " << name << endl;
return 0;
}