Trying to make a quick little tool in response to the FBI's request for assistance on cracking a code.. getting an error that takes me outside the scope of my program.. not sure what is causing the problemo. Maybe another set of eyes can help:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <vector>
#include <algorithm>

using namespace std;

class Decode
{
    public:

    char menu();
    void display();
    void load(ifstream);
    void eliminate();
    void substitue();
    void rand_shuffle();
    void save(ofstream);

    private:

    vector<char> file;
};

int main()
{
    Decode codebreak;
    ifstream infile;
    ofstream outfile;
    string name;
    char again = '\0';

    cout << "Enter file location and name to open: ";
    cin >> name;

    //open file
    infile.open(name.c_str());

    //error handling
    if(!infile.is_open())
    {
        cout << "\a\n\nError! File could not be opened! ";
        cout << "\n(incorrect path...  file possibly missing, renamed, or relocated) ";
        exit(1);
    }

    //load file
    codebreak.load(infile);

    //main driver
    do
    {
        again = codebreak.menu();

    }while(again != '6');

    //cleanup
    infile.close();

    return 0;
}

void Decode::load(ifstream infile)
{
    char c = '\0';

    file.clear();

    while(infile)
    {
        infile.get(c);
        file.push_back(c);
    }
}

char Decode::menu()
{
    char select = '\0';

    cout << "\n\n\n1. Substitue";
    cout << "\n2.  Eliminate";
    cout << "\n3.  Random Shuffle\n";
    cout << "\n4.  Reload Original\n";
    cout << "\n5.  Save Results\n";
    cout << "\n6.  Quit\n\n";

    cin >> select;

    if(!isdigit(select) || isdigit < 1 && isdigit > 3)
    {
        cout << "\n\n\aInvalid entry, try again: ";
        menu();
    }

    switch(select)
    {
        case 1:  substitute();
        break;
        case 2:  eliminate();
        break;
        case 3:  rand_shuffle();
        break;
        case 4:  load(ifstream);
        break;
        case 5:  save(ofstream);
        break;
    }

    display();

    return select;
}

void Decode::substitute()
{
    char target = '\0';
    char sub = '\0';

    cout << "\n\nEnter character ye' wish to sub: ";
    cin >> target;

    cout << "\n\nEnter substitution character: ";
    cin >> sub;

    replace(file.begin(), file.end(), target, sub);
}

void Decode::eliminate()
{
    char target = '\0';

    cout << "\n\nEnter character ye' wish to eliminate: ";
    cin >> target;

    remove(file.begin(), file.end(), target);
}

void Decode::rand_shuffle()
{
    random_shuffle(file.begin(), file.end());
}

void Decode::save(ofstream outfile)
{
    string name;

    cout << "Enter save file path & document name: ";
    cin >> name;

    outfile.open(name.c_str());

    for(int i=0, size=file.size(); i<size; i++)
    {
        outfile << file[i];

        if(!(i%80))
        {
            outfile << endl;
        }
    }

    outfile.close();
}

void Decode::display()
{
    for(int i=0, size=file.size(); i<size; i++)
    {
        cout << file[i];

        if(!(i%80))
        {
            cout << endl;
        }
    }
}

Edited 5 Years Ago by Clinton Portis: n/a

line 92: isdigit is a macro that returns a bool value (true or false, 1 or 0) You can not compare it with a value as you have tried to do in that line.

Other errors I found are just careless errors, such as misspelled function names. Check the error messages better and you will be able to correct them.

ifstream and ofstream objects have to be passed by reference, not by value, like this line: void load(ifstream& infile); But that is also going to get you into deep trouble because the streams were already opened in main() and your program is trying to open them again in load() and save(). Do that is just one of the two places, but not both. Since the streams declared in main() are not used for anything just don't pass any parameters to load() and save(), and declare the stream objects in those functions.

Edited 5 Years Ago by Ancient Dragon: n/a

Comments
muy bien

Thank ye' dragon of ancientness.. i think the main problem I ran into is forgetting to pass fstream objects by reference. aside from that, a few typos and I be good to go.

Edited 5 Years Ago by Clinton Portis: n/a

Thank ye' dragon of ancientness.. i think the main problem I ran into is forgetting to pass fstream objects by reference. aside from that, a few typos and I be good to go.

No, that is not all the problems. Read the rest of my post where I mentioned opening the files in two different places. You can't do that.

I was wondering if ya'll could take a look at my load() function (line #76)... it works fine the first time, but any attempt to reload the document into the file vector causes the program to crash:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <vector>
#include <algorithm>

using namespace std;

class Decode
{
    public:

    Decode(){ctotal = ltotal = caverage = 0;}
    int menu(ifstream&, ofstream&);
    void doc_stats();
    void display();
    void load(ifstream&);
    void eliminate();
    void substitute();
    void rand_shuffle();
    void save(ofstream&);

    private:

    vector<char> file;
    int ctotal;
    int ltotal;
    double caverage;
};

int main()
{
    Decode codebreak;
    ifstream infile;
    ofstream outfile;
    string name;
    int again = 0;
/*
    cout << "Enter file path and name to open: ";
    cin >> name;
*/

    //open file
    infile.open("C:\\Users\\Bitch Slap\\Documents\\resume.txt");

    //error handling
    if(!infile.is_open())
    {
        cout << "\a\n\nError! File could not be opened! ";
        cout << "\n(incorrect path...  file possibly missing, renamed, or relocated) ";
        exit(1);
    }

    //load file
    codebreak.load(infile);

    //initial display
    codebreak.display();

    //main driver
    do
    {
        again = codebreak.menu(infile, outfile);

    }while(again != 6);

    //cleanup
    infile.close();
    outfile.close();

    return 0;
}

void Decode::load(ifstream& infile)
{
    char c = '\0';

    file.clear();
    infile.seekg(0, ios::beg);

    while(infile)
    {
        infile.get(c);
        file.push_back(c);
    }
}

int Decode::menu(ifstream& infile, ofstream& outfile)
{
    int selection = 0;

    doc_stats();

    cout << "\n\n\n|------------------------|";
    cout << "\n|          Menu          |";
    cout << "\n|------------------------|";
    cout << "\n| 1.  Substitute         |";
    cout << "\n| 2.  Eliminate          |";
    cout << "\n| 3.  Random Shuffle     |";
    cout << "\n| 4.  Reload Original    |";
    cout << "\n| 5.  Save Results       |";
    cout << "\n| 6.  Quit               |";
    cout << "\n|------------------------|";
    cout << "\n char total: " << ctotal;
    cout << "\n line total: " << ltotal;
    cout << "\n ave. char/line: " << caverage;
    cout << "\n|------------------------|";
    cout << "\n\nSelection:  ";


    cin >> selection;

    if(selection < 1 && selection > 6)
    {
        cout << "\n\n\aInvalid entry, try again: ";
        menu(infile, outfile);
    }

    switch(selection)
    {
        case 1:  substitute();
        break;
        case 2:  eliminate();
        break;
        case 3:  rand_shuffle();
        break;
        case 4:  load(infile);
        break;
        case 5:  save(outfile);
        break;
    }

    display();

    return selection;
}

void Decode::doc_stats()
{
    ctotal = ltotal = caverage = 0;

    for(int i=0, size=file.size(); i<size; i++)
    {
        if(!isspace(file[i]))
        {
            ctotal++;
        }
    }

    ltotal = count(file.begin(), file.end(), '\n');
    caverage = ctotal / ltotal;
}

void Decode::substitute()
{
    char target = '\0';
    char sub = '\0';

    cout << "\n\nEnter character ye' wish to sub: ";
    cin >> target;

    cout << "\n\nEnter substitution character: ";
    cin >> sub;

    replace(file.begin(), file.end(), target, sub);
}

void Decode::eliminate()
{
    char target = '\0';

    cout << "\n\nEnter character ye' wish to eliminate: ";
    cin >> target;

    remove(file.begin(), file.end(), target);

}

void Decode::rand_shuffle()
{
    random_shuffle(file.begin(), file.end());
}

void Decode::save(ofstream& outfile)
{
    string name;

    cout << "Enter save file path & document name: ";
    cin >> name;

    outfile.open(name.c_str());

    for(int i=0, size=file.size(); i<size; i++)
    {
        outfile << file[i];

        if(!(i%80))
        {
            outfile << endl;
        }
    }
}

void Decode::display()
{
    for(int i=0, size=file.size(); i<size; i++)
    {
        cout << file[i];

    }
}

I've declared fstream objects in main, then use them once to open documents by calling the open() function (line #46 & #193 are the only two calls to open() ).

Edited 5 Years Ago by Clinton Portis: n/a

Walt is correct, you are leaving those files open. That's won't happen if you would remove the parameters and declare the streams inside the functions that use them, as I suggested over 4 hours ago.

Thank ye' all for ya'lls input. Got a descent program going. If anyone is interested in joining the fight against crime, I found a pretty good text version of the writing samples provided by the FBI:

(MND MK NEA 8SE-N-S-M-KNA8E) (A(SMS))

?TFRNE N'?tNSE NPBSE R CBBNSE NPRSE INC

PRSE N MRSE OPRE HLD WLD NCBE (TFXLF TCXL NCBE)

AL-PRPPIT XLY PPIY NCBE MGK SE WLD RCB R NSE PRSE

WLD RCBR N SE N T OBNEN TXSE-C85LE-CLTRSE WLD NCBE

AL WLD N CBET5ME LISE RLSE v R GLSN-E AS N WLD NCBE

(NO PFSE N L S RE NCBE) NTE G D DMNSEN C U RE RCBRNE

(TENE TFRNE NCBR TSE N CBE INQ)

(FLRSE PRSE ONDE 71 NCBE)

(CDNSE PRSE ON5DE 74 NCBE)

(PR+SE PRSE ON RE DE 75 NCBE)

(TF N QCMSP SOLE MRDE LUSE TOTE WLD N WLD NCBE)

(194 WLD'S NCBE) (TRFXL)

NOTES

AL PNTE GLSE — SE ER+E

YLSE MTSE-CTSE-WSE-FRTSE

PNR TRSE O N PRSE WLD NCBE

N WLD XL R CMSP NE WLD STS ME XL

DULMT 6 TUNSE NCBE XL

end section

(MU NSASISTEN MU NARSE)

KLSE-LRSTE-TR SE-TRSE-MKSEN-MRSE

(SAE 6 NSE SE N MBSE)

end section

NMNRCBR NSE P+E 2PTE WS RC BREEE

36 MLSE 74 SPRKSE 29KE NO8, OLE 173RTRSE

35 6LE CLGSE OUNVTREDKRSE PSESHLE

651 MTLSE HTLSE N CUTCTRS NMRF

99.84.5 2UNEPLSE NCRSE AOLTSE NSKSE NB SE

NSRE ONSE PVT SE WLD NCBE (3XORL)

end section

PNMSE NRSE 1 NR NTRLERCB ANSE NTSRCR6 NE

LSPNSE N GSPSE MKSE R BSE N CBE AV XLR

HM CRE N MRE NCBE 1/2 MUNDPLSE

end section

D-W-M14MIL XDRLX

And here is me' working program:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <vector>
#include <algorithm>

using namespace std;

class Decode
{
    public:

    Decode(){ctotal = ltotal = caverage = tot_offs = show_num = 0;}
    int menu(ifstream&, ofstream&);
    void doc_stats();
    void display();
    void load(ifstream&);
    void substitute();
    void eliminate();
    void offset();
    void rand_shuffle();
    void show_numbers();
    void save(ofstream&);

    private:

    vector<char> file;
    int ctotal;
    int ltotal;
    int tot_offs;
    double caverage;
    bool show_num;
};

int main()
{
    Decode codebreak;
    ifstream infile;
    ofstream outfile;
    string name;
    int again = 0;
/*
    cout << "Enter file path and name to open: ";
    cin >> name;
*/

    //open file
    infile.open("C:\\Users\\Bitch Slap\\Desktop\\code4.txt");

    //error handling
    if(!infile.is_open())
    {
        cout << "\a\n\nError! File could not be opened! ";
        cout << "\n(incorrect path...  file possibly missing, renamed, or relocated) ";
        exit(1);
    }

    //load file
    codebreak.load(infile);

    //initial display
    codebreak.display();

    //main driver
    do
    {
        again = codebreak.menu(infile, outfile);

    }while(again != 8);

    //cleanup
    infile.close();
    outfile.close();
    return 0;
}

void Decode::load(ifstream& infile)
{
    char c = '\0';

    file.clear();
    infile.seekg(0, ios::beg);

    while(infile)
    {
        infile.get(c);
        file.push_back(c);
    }

    infile.clear();

    tot_offs = 0;
}

int Decode::menu(ifstream& infile, ofstream& outfile)
{
    int selection = 0;

    doc_stats();

    cout << "\n\n\n|------------------------|";
    cout << "\n|          Menu          |";
    cout << "\n|------------------------|";
    cout << "\n| 1.  Substitute         |";
    cout << "\n| 2.  Eliminate          |";
    cout << "\n| 3.  Apply Offset       |";
    cout << "\n| 4.  Random Shuffle     |";
    cout << "\n| 5.  Enum. Line Toggle  |";
    cout << "\n| 6.  Reload Original    |";
    cout << "\n| 7.  Save Results       |";
    cout << "\n| 8.  Quit               |";
    cout << "\n|------------------------|";
    cout << "\n char total: " << ctotal;
    cout << "\n line total: " << ltotal;
    cout << "\n ave. char/line: " << caverage;
    cout << "\n Offset # " << tot_offs;
    cout << "\n|------------------------|";
    cout << "\n Selection:  ";


    cin >> selection;

    if(selection < 1 && selection > 6)
    {
        cout << "\n\n\aInvalid entry, try again: ";
        menu(infile, outfile);
    }

    switch(selection)
    {
        case 1:  substitute();
        break;
        case 2:  eliminate();
        break;
        case 3:  offset();
        break;
        case 4:  rand_shuffle();
        break;
        case 5:  show_numbers();
        break;
        case 6:  load(infile);
        break;
        case 7:  save(outfile);
        break;
    }

    display();

    return selection;
}

void Decode::doc_stats()
{
    ctotal = ltotal = caverage = 0;

    for(int i=0, size=file.size(); i<size; i++)
    {
        if(!isspace(file[i]))
        {
            ctotal++;
        }
    }

    ltotal = count(file.begin(), file.end(), '\n');
    caverage = ctotal / ltotal;
}

void Decode::substitute()
{
    char target = '\0';
    char sub = '\0';

    cout << "\n\nEnter character ye' wish to sub: ";
    cin >> target;

    cout << "\n\nEnter substitution character: ";
    cin >> sub;

    replace(file.begin(), file.end(), target, sub);
}

void Decode::eliminate()
{
    char target = '\0';

    cout << "\n\nEnter character ye' wish to eliminate: ";
    cin >> target;

    remove(file.begin(), file.end(), target);

}

void Decode::offset()
{
    for(int i=0, size=file.size(); i<size; i++)
    {
        if(isalpha(file[i]))
        {
           file[i]++;

            if(file[i] > 'Z')
            {
                file[i] = 'A';
            }
        }
    }

    tot_offs++;
}

void Decode::rand_shuffle()
{
    random_shuffle(file.begin(), file.end());
}

void Decode::save(ofstream& outfile)
{
    string name;

    cout << "Enter save file path & document name: ";
    cin >> name;

    outfile.open(name.c_str());

    for(int i=0, size=file.size(); i<size; i++)
    {
        outfile << file[i];
    }
}

void Decode::show_numbers()
{
     if(show_num)
     {
         show_num = false;
     }
     else
     {
         show_num = true;
     }
}

void Decode::display()
{
    int line_count = 1;

    for(int i=0, size=file.size(); i<size; i++)
    {
        cout << file[i];

        if(show_num && file[i] == '\n')
        {
            cout << line_count << ". ";
            line_count++;
        }

    }
}

Edited 5 Years Ago by Clinton Portis: n/a

This question has already been answered. Start a new discussion instead.