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 7628819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:40:16+00:00 2026-05-31T05:40:16+00:00

I have built a Program to decipher shift ciphers (Cesar Cipher). It seems to

  • 0

I have built a Program to decipher shift ciphers (Cesar Cipher). It seems to take the Input and makes a output file but it is empty. I am thinking that something may be wrong with the Deciphering function and have tried changing multiple things to no avail. I use Bloodshed C++ for compiling and windows for a OS.

Thanks

kd7vdb

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
const int ARRAYSIZE = 128;

void characterCount(char ch, int list[]);
void calcShift( int& shift, int list[]);
void writeOutput(ifstream &in, ofstream &out, int shift);

int main()
{
    int asciiCode = 0,
        shift = 0;
    string filename;
    char ch;
    ifstream infile;
    ofstream outfile;

    //input file

    cout << "Input file name: ";
    getline(cin, filename);

    infile.open(filename.c_str());

        if (!infile.is_open()) { 

            cout << "Unable to open file or it doesn't exist." << endl;

            return 1;

        }

    //output file

    cout << "Output file name: ";
    getline(cin, filename);



    outfile.open(filename.c_str());

    int list[ARRAYSIZE] = {0}; 

        while (infile.peek() != EOF) 
        {
            infile.get(ch);
            characterCount(ch, list); 
        }



    infile.clear();
    infile.seekg(0);

    calcShift (shift, list); //Calculate the shift based on the <strong class="highlight">most</strong> characters counted
    writeOutput(infile, outfile, shift); //Decypher and write to the other document

    return 0;
}

void characterCount(char ch, int list[])
{
        if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
        {
            int asciiCode = 0;

            asciiCode = static_cast<int>(ch); //Change it to the ASCII number
            list[asciiCode]++; //And note it on the array
        }
}

void calcShift( int& shift, int list[])
{
    int maxIndex = 0, //Asuming that list[0] is the largest
        largest = 0;

        for (int i = 1; i < ARRAYSIZE; i++)
        {
            if (list[maxIndex] < list[i])
                    maxIndex = i; //If this is true, change the largest index
        }

    largest = list[maxIndex]; //When the maxIndex is found, then that has the largest number.

        if (largest >= 65 && largest <= 90) //Calculate shift with <strong class="highlight">E</strong> (for upper-case letters)
            shift = largest - 69;

        if (largest >= 97 && largest <= 122) //For lower-case letters (<strong class="highlight">e</strong>)
            shift = largest - 101;
}

void writeOutput(ifstream &infile, ofstream &outfile, int shift)
{
    char ch;
    int asciiCode = 0;

        while (infile.peek() != EOF) { //Until it is the end of the file...

            infile.get(ch); //Get the next character

                if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
                {
                    asciiCode = static_cast<int>(ch); //Change it to the ASCII number
                    asciiCode += shift; //Do the shift
                    ch = static_cast<char>(asciiCode); //Change it to the shifted letter
                }

            outfile << ch; //Print to the outfile
        }
}
  • 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-31T05:40:18+00:00Added an answer on May 31, 2026 at 5:40 am

    You have a couple of logical errors. I think your last two functions should be more like:

    void calcShift( int& shift, int list[])
    {
        int maxIndex = 0; //Asuming that list[0] is the largest
    
        for (int i = 1; i < ARRAYSIZE; i++)
        {
            if (list[maxIndex] < list[i])
                    maxIndex = i; //If this is true, change the largest index
        }
    
        if (maxIndex >= 'A' && maxIndex <= 'Z') //Calculate shift with <strong class="highlight">E</strong> (for upper-case letters)
            shift = 'E' - maxIndex;
    
        if (maxIndex >= 'a' && maxIndex <= 'z') //For lower-case letters (<strong class="highlight">e</strong>)
            shift = 'e' - maxIndex;
    }
    
    void writeOutput(ifstream &infile, ofstream &outfile, int shift)
    {
        char ch;
    
        while (infile.peek() != EOF) { //Until it is the end of the file...
    
            infile.get(ch); //Get the next character
    
                if (ch >= 'A' && ch <= 'Z') //If the character is in the alphabet...
                {
                    ch = 'A' + (((ch - 'A') + shift + 26) % 26);
                }
                if (ch >= 'a' && ch <= 'z') //If the character is in the alphabet...
                {
                    ch = 'a' + (((ch - 'a') + shift + 26) % 26);
                }
    
            outfile << ch; //Print to the outfile
        }
    }
    

    To summarise, you don’t need largest in calcShift, you need to subtract maxIndex from 'E' or 'e' to calculate the shift, and your calculation of the substituted char in writeOutput was pretty far off.

    I’m not sure why you were getting an empty output file, but this works for me using MSVC.

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

Sidebar

Related Questions

I have to build a C# program that makes CSV files and puts long
We have a custom built program that needs authenticated/encrypted communication between a client and
So, I have built an auto update program to my program. The code that
I have built setup file for my windows service in visual studio 2010.Its working
I have a program built with VB6 and using some 32-bit DLL's . Will
all. I have built a simple jQuery/PHP chat program that works rather well. However,
I have built a small program for school that runs perfectly when i run
I have built a family tree program which thus far allows you to add
I have built a program in Microsoft Visual Studio 2005 and it works fine.
I have built OpenSSL for Android(using ndk-build) and have linked it to my program.

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.