Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:26:25+00:00 2026-05-29T11:26:25+00:00

REVISED: Here is my entire compilable program. It is menu driven, but the part

  • 0

REVISED: Here is my entire compilable program. It is menu driven, but the part that I am stuck on is option DECRYPT, to decrypt a caesar cyphered file, or number 5 (either could be typed in at the initial question, the decrypt could be lower case, upper case, or camel case). The ofstream variable outFile creates a file named by the user (must be a non-existing file). The problem is that it only creates the empty file, and does not print any of the data into it. All of the variables store the correct values. cout works, but outFile does not. Is there something that I am not doing correctly? I have tried to test for bad, fail, and is_open and none of them have any trouble. I do not think that file permissions would prevent anything either, since the other options in the program create and write to a file just fine. Can anyone help me?

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
#include <vector>
using namespace std;
int main() {
string inputFileName, outputFileName, inData, outData, inWord, outWord, trash;
ifstream inFile, testStream;
ofstream outFile;
bool outPutOpened = false, isLowerCase = false;
char outChar, inChar; 
int shiftNum = 0, idx = 0, total = 0, max = 0, shiftValue = 0;
map<char,int> charMap; 
map<char,int>::iterator mapIt; 
vector<char> alphabet(26); 
vector<char>::iterator shiftIt;
do  {
    inWord.clear();
    outWord.clear();
    cout << "Available options: " << endl;
    cout << "1. ENCRYPT - Encrypt a file using Caesar Cypher" << endl
        << "2. CHARFREQ - display character frequency table for file" 
        << endl << "3. Quit - Exit the program" << endl 
    << "5. DECRYPT - decrypt a cyphered file" << endl << endl;
    cout << "   Enter keyword or option index: ";
    getline(cin, inWord);
    outWord.resize(inWord.length());
    transform(inWord.begin(), inWord.end(), outWord.begin(), ::toupper); 
    if (outWord.compare("ENCRYPT") == 0 || outWord.compare("1") == 0) {
        cout << "CAESAR CYPHER PROGRAM" << endl
        << "======================" << endl << endl;
        do {                
            cout << "Provide the input file name: ";

            getline(cin, inputFileName);

            inFile.open(inputFileName.c_str());
            if (inFile.fail()) {
                cout << "Cannot open file, please try again!" << endl;
                inFile.clear();
            }
        }
        while (!inFile.is_open());

        do {
            cout << "Provide the output file name: ";
            cin >> outputFileName;
            getline(cin, trash);                
            testStream.clear();
            testStream.open(outputFileName.c_str());
            if(testStream.good()) {
                cout << "That file already exists, choose another" << endl;
                testStream.clear();
                testStream.close();
            }
            else {
                testStream.clear();
                testStream.close();
                outFile.open(outputFileName.c_str());
                if (outFile.good()) {
                    outPutOpened = true;
                }   
            }
        }
        while (!outPutOpened); 
        cout << "Enter the shift number: ";
        cin >> shiftNum;
        getline(cin, trash );
        while(getline(inFile, inData)) {

        for (idx = 0; idx <= inData.length() - 1; idx++) {
            if  (inData[idx] >= 'a' && inData[idx] <= 'z') {
                outChar = (((inData[idx] - 'a') + shiftNum) % 26) + 'a';
                outFile.put(outChar);
            }
            else if (inData[idx] >= 'A' && inData[idx] <= 'Z'){
                outChar = (((inData[idx] - 'A') + shiftNum) % 26) + 'A'; 
                outFile.put(outChar);
            }
            else {
                outFile.put(inData[idx]);
            }
        }
        }
        inFile.clear();
        inFile.close();
        outFile.clear();
        outFile.close();
    }
    else if (outWord.compare("2") == 0 || outWord.compare("CHARFREQ") == 0){
        cout << "Enter input file name: ";
        getline(cin, inputFileName);
        inFile.open(inputFileName.c_str());
        while (inFile.get(inChar)) {
            if (charMap.find(inChar) == charMap.end()) {
                charMap[inChar] = 1 ;
            }
            else {
                ++charMap[inChar];
            }
        }
        cout << "Character Frequencies For \"" << inputFileName << "\"" 
            << endl;
        for (mapIt = charMap.begin(); mapIt != charMap.end(); mapIt++) {
            total += (*mapIt).second;
        }
        cout << "Total bytes read: " << total << endl;
        for (mapIt = charMap.begin(); mapIt != charMap.end(); mapIt++) {
            cout << " ('" << (*mapIt).first << "') occurs " 
            << (*mapIt).second << " times (" 
            << static_cast<double> ((*mapIt).second) 
            / static_cast<double> (total) << "% of all characters)" << endl;
        }
        inFile.clear();
        inFile.close();
    }
    else if (outWord.compare("5") == 0|| outWord.compare("DECRYPT") == 0) {
        outPutOpened = false;
        do {                
            cout << "Provide the input file name: ";

            getline(cin, inputFileName);

            inFile.open(inputFileName.c_str());
            if (inFile.fail()) {
                cout << "Cannot open file, please try again!" << endl;
                inFile.clear();
            }
        }
        while (!inFile.is_open());          
        while (inFile.get(inChar)) {
            if (inChar < 'a' || inChar > 'z') {
                inFile.ignore();
            }
            else {
                inChar -= 'a'; 
                alphabet[static_cast<int> (inChar)]++;
            }
        }
        for (idx = 0; idx < alphabet.size(); idx++) {
            if(max < alphabet[idx]){
                max = alphabet[idx];
            }
        }
        shiftIt = find(alphabet.begin(), alphabet.end(), max);

        shiftValue = (distance(alphabet.begin(), shiftIt) - 4);
        if (shiftValue < 0) {
            shiftValue += 26;
        }
        inFile.close();
        do {                
            inFile.open(inputFileName.c_str());
            if (inFile.fail()) {
                cout << "Cannot open file, please try again!" << endl;
                inFile.clear();
            }
        }
        while (!inFile.is_open());

        outPutOpened = false;
        do {
            cout << "Provide the output file name: ";
            cin >> outputFileName;
            getline(cin, trash);                
            testStream.clear();
            testStream.open(outputFileName.c_str());
            if(testStream.good()) {
                cout << "That file already exists, choose another" << endl;
                testStream.clear();
                testStream.close();
            }
            else {
                testStream.clear();
                testStream.close();
                outFile.open(outputFileName.c_str());
                if (!outFile.good()) {
                    cout << "bad output"<< endl;
                    outFile.clear();
                }   
                if (outFile.good()) {
                    outPutOpened = true;
                }
            }
        }
        while(!outPutOpened); 

        while((inFile.get(inChar))) {

            if  (inChar >= 'a' && inChar <= 'z') {


                inChar -= shiftValue;
                if (inChar < 'a') {
                    inChar += 26;
                }

            outFile << inChar;
            }
            else if (inChar >= 'A' && inChar <= 'Z'){
                inChar -= shiftValue;
                if (inChar < 'A') {
                    inChar += 26;
                }
                outFile << inChar;   

            }
            else {
                outFile << inChar;
            }

        }
    }
    else if (outWord.compare("3") == 0 || outWord.compare("QUIT") == 0) {
        break;
    }
    else {
        cout << inWord << " is an unrecognized option, please try again" 
        << endl;
    }
}
while (outWord.compare("3") || outWord.compare("QUIT")); 
return 0;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:26:27+00:00Added an answer on May 29, 2026 at 11:26 am

    you have to flush the stream in order for the chars to actually be written to file.
    after your write-to-outFile-while loop do a:

    outFile.flush();
    

    .. and the text will be written fine to the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Didn't mean to create a new question, but revised the old one enough that
REVISED QUESTION (SEE BELOW FOR ORIGINAL): Here is an example of a simple ajax
Check the bottom for revised edition Alright, here's the issue. I have a li
REVISED QUESTION I have revised the original question (as seen below) so that I
For clarification purposes I need the program to print the numbers that are input
I'm attempting to call startActivity(myIntent) from the click of a menu button but my
Revised Code jQuery(function($) { $(#filter).keyup(function () { var filter = $(this).val(), count = 0;
REVISED QUESTION : We have tracked this down to a custom add to cart
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Here is a real version control system dummy! proper new starter! The way I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.