0
//I got almos this program done, it gets the apropiate result skipping some steps.
//How do i pass the "int diff" to the "printText function".  diff is the largest elemt or the 
//more repetitive letter from a text file, where the alphabet was shifted by 12.
//Also, The largest element is in the position 17 which is Q in the alphabet, so E is 
//position 5.  "diff = 17-5". How do i get a formula so diff always is the difference beteween
//larget element and E, to decrypt any code.

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

using namespace std;

//Function proptotype
//void managingFiles(string& source, string& destination);
void initialize(int list[]);
void printText(ifstream& intext, ofstream& outtext, char& ch);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
             int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int list[]);

int main()
{
    //Declare variables
    int letterCount[26];
    char ch, diff;
    ifstream infile; 
    ofstream outfile;
     
string data, source, destination;
        

//managingFiles(source, destination);

//file management
//infile.open(source.c_str());
 infile.open("C:\\input.txt");
if (!infile)                                    //Step 3
    {
        cout << "Cannot open the input file." << source
             << endl;
        return 1;
    }      
//outfile.open(destination.c_str());
 outfile.open("C:\\output.txt");
initialize(letterCount);

infile.get(ch);
//set the while loop     
 while (infile)                                  //Step 7
    {
        copyText(infile, outfile, ch, letterCount); //Step 7.1
    
        infile.get(ch);                             //Step 7.3
    }
    infile.clear();
infile.seekg(0);
 infile.get(ch);
 while (infile)                                  //Step 7
    {
        printText(infile, outfile, ch); //Step 7.1
        //copyText(infile, outfile, ch, letterCount);
        infile.get(ch);                             //Step 7.3
    }   
    
    writeTotal(outfile, letterCount); 
//close files
         infile.close();
         outfile.close();     
         
 return 0;
}
void printText(ifstream& intext, ofstream& outtext, char& ch)
{
     int diff = 12;
    while (ch != '\n')
    {
        int offset, y;

        int x = static_cast<int> (ch);
        if (x >= 65 && x <= 90)
        {
            y = ( (static_cast<int> (ch) - static_cast<int> ('A') ) - diff); 
            offset = static_cast<int> ('A');
        }
        else if (x >= 97 && x <= 122)
        {        
            y = ( (static_cast<int> (ch) - static_cast<int> ('a') ) - diff);
            offset = static_cast<int> ('a');
        }
        else
        {
            outtext << ch;

            intext.get (ch);

            continue;
        }

        char c;
        if (y < 0)
            c = y + 26 + offset;
        else
            c = y + offset;

        outtext << c;

        intext.get (ch);
    }

    outtext << ch;
}
void writeTotal(ofstream& outtext,int list[])
{
    int index;
    int maxIndex = 0;
    int count = 0;
    int diff = 0;

    outtext << endl << endl;
  
    for (index = 0; index < 26; index++)
       
        outtext << static_cast<char>(index + static_cast<int>('A'))
        
                << " count = " << list[index] << endl;
       
        for (index = 0; index < 26; index++){
        
        if (list[maxIndex] < list[index])
           maxIndex = index;
           }
           outtext 
                << " largest element is E = " << maxIndex << endl;
         
                 diff = maxIndex;
} //end writeTotal
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by William Hemsworth
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.