0

Hey forum, I'm very new to C++, and I need some help. I'm currently having trouble trying to write a Caesar cipher in C++. So far, I'm able to get it to read characters from a text file, and let it count the # each letter appears in an array. What I'm trying to do exactly is, find the most frequent letter, and then use that to find the shift. But before that, i want to display which letter that it was that was most frequent, and the number of times it was in the text.

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

using namespace std;

//Globals
const int ARRAY_SIZE = 128;
const int E_LOC = 4;
int list[ARRAY_SIZE] = {0};
string reply;
ofstream outputFileName;
ifstream inputFile;
char ch;
	
//Prototypes
void characterCount(char ch, int list[]); 
void calcShift( int& shift, int list[]); 
void writeOutput(ifstream &in, ofstream &out, int shift);

int main() {
	int shift = 0;
    string inputFileName;
    char ch;
	 
	//Input File Name
    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());
    
	// Check the file opened successfully.
	if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }
	//Open the output file.
	cout << "Output file name: ";
	getline(cin, inputFileName);
	outputFileName.open(inputFileName.c_str());

    // This section reads and echo's the file one character (byte) at a time.
    while (inputFile.peek() != EOF) 
	{
        inputFile.get(ch);
		characterCount(ch, list);
    }

	calcShift(shift, list);

	inputFile.clear();
    inputFile.seekg(0);

	inputFile.close();
	inputFile.open(inputFileName.c_str());   

	cout << endl;
	system("PAUSE");
		return 0;
}

void characterCount(char ch, int list[])
{
		if (ch >= 'a' && ch <= 'z')
		{
			int asciiCode = 0;
 
			asciiCode = (static_cast<int>(ch)); 
			list[asciiCode]++; 
			cout << ch;
		}
}


void calcShift( int& shift, int list[])
{
	int mostUsedLetter = list[0];
	int mUsedIndex;

//Loop to search for greatest count and location (index) of greatest count.
for(int i = 0; i < ARRAY_SIZE; i++) 
	{
		if(list[i] > mostUsedLetter)
		{
			mostUsedLetter = list[i];
			mUsedIndex = i;
			cout << endl << "The letter: " << static_cast<char>(mUsedIndex) << endl << " was shown " << mostUsedLetter << " times." << endl;
			//shift = mUsedIndex - E_LOC;
		}
		
	}
}

I just threw up a bunch of default headers first, and worked from there. This code isn't complete yet, but I'm going step by step and trying to fix my mistakes before i continue. The current problem I'm having at the moment is that when i execute the code, it displays all the characters normally, but at the point where i prompt it to display the most frequent letter, it displays the message twice. One for a and l. Why is it doing that? Feel free to flame me with feedback, I really need it. Advice on waht to do next would be nice too.

Edited by MetallicKaiser: n/a

Attachments
Jhss tl Pzothls. Zvtl flhyz hnv - ulcly tpuk ovd svun wyljpzlsf - ohcpun spaasl vy uv tvulf pu tf wbyzl,
huk uvaopun whyapjbshy av pualylza tl vu zovyl, P aovbnoa P dvbsk zhps hivba h spaasl huk zll aol dhalyf
whya vm aol dvysk. Pa pz h dhf P ohcl vm kypcpun vmm aol zwsllu, huk ylnbshapun aol jpyjbshapvu. Dolulcly
P mpuk tfzlsm nyvdpun nypt hivba aol tvbao; dolulcly pa pz h khtw, kypggsf Uvcltily pu tf zvbs; dolulcly
P mpuk tfzlsm pucvsbuahypsf whbzpun ilmvyl jvmmpu dhylovbzlz, huk iypunpun bw aol ylhy vm lclyf mbulyhs P
tlla; huk lzwljphssf dolulcly tf ofwvz nla zbjo hu bwwly ohuk vm tl, aoha pa ylxbpylz h zayvun tvyhs
wypujpwsl av wylclua tl myvt klspilyhalsf zalwwpun puav aol zaylla, huk tlaovkpjhssf ruvjrpun wlvwsl'z
ohaz vmm - aolu, P hjjvbua pa opno aptl av nla av zlh hz zvvu hz P jhu. Aopz pz tf zbizapabal mvy wpzavs
huk ihss. Dpao h wopsvzvwopjhs msvbypzo Jhav aoyvdz optzlsm bwvu opz zdvyk; P xbplasf ahrl av aol zopw.
Aolyl pz uvaopun zbywypzpun pu aopz. Pm aolf iba ruld pa, hstvza hss tlu pu aolpy klnyll, zvtl aptl vy vaoly,
jolypzo clyf ulhysf aol zhtl mllspunz avdhykz aol vjlhu dpao tl. 
Aolyl uvd pz fvby puzbshy jpaf vm aol Thuohaavlz, ilsalk yvbuk if dohyclz hz Pukphu pzslz if jvyhs yllmz -
jvttlyjl zbyyvbukz pa dpao oly zbym. Ypnoa huk slma, aol zayllaz ahrl fvb dhalydhyk. Paz leayltl kvdu-avdu pz
aol ihaalyf, dolyl aoha uvisl tvsl pz dhzolk if dhclz, huk jvvslk if iyllglz, dopjo h mld ovbyz wylcpvbz dlyl
vba vm zpnoa vm shuk. Svvr ha aol jyvdkz vm dhaly-nhglyz aolyl. 
Jpyjbthtibshal aol jpaf vm h kylhtf Zhiihao hmalyuvvu. Nv myvt Jvyslhyz Ovvr av Jvluaplz Zspw, huk myvt aolujl,
if Dopalohss uvyaodhyk. Doha kv fvb zll? - Wvzalk sprl zpslua zluapulsz hss hyvbuk aol avdu, zahuk aovbzhukz bwvu
aovbzhukz vm tvyahs tlu mpelk pu vjlhu ylclyplz. Zvtl slhupun hnhpuza aol zwpslz; zvtl zlhalk bwvu aol wply-olhkz;
zvtl svvrpun vcly aol ibsdhyrz vm zopwz myvt Jopuh; zvtl opno hsvma pu aol ypnnpun, hz pm zaypcpun av nla h zapss
ilaaly zlhdhyk wllw. Iba aolzl hyl hss shukztlu; vm dllr khfz wlua bw pu shao huk wshzaly - aplk av jvbualyz,
uhpslk av ilujolz, jspujolk av klzrz. Ovd aolu pz aopz? Hyl aol nyllu mplskz nvul? Doha kv aolf olyl? 
Iba svvr! olyl jvtl tvyl jyvdkz, whjpun zayhpnoa mvy aol dhaly, huk zlltpunsf ivbuk mvy h kpcl. Zayhunl! Uvaopun
dpss jvualua aolt iba aol leayltlza sptpa vm aol shuk; svpalypun bukly aol zohkf sll vm fvukly dhylovbzlz dpss uva
zbmmpjl. Uv. Aolf tbza nla qbza hz upno aol dhaly hz aolf wvzzpisf jhu dpaovba mhsspun pu. Huk aolyl aolf zahuk -
tpslz vm aolt - slhnblz. Pushuklyz hss, aolf jvtl myvt shulz huk hsslfz, zayllaz huk hclublz, - uvyao, lhza, zvbao,
huk dlza. Fla olyl aolf hss bupal. Alss tl, kvlz aol thnulapj cpyabl vm aol ullkslz vm aol jvtwhzzlz vm hss aovzl
zopwz haayhja aolt aopaoly? 
Vujl tvyl. Zhf, fvb hyl pu aol jvbuayf; pu zvtl opno shuk vm shrlz. Ahrl hstvza huf whao fvb wslhzl, huk alu av vul
pa jhyyplz fvb kvdu pu h khsl, huk slhclz fvb aolyl if h wvvs pu aol zaylht. Aolyl pz thnpj pu pa. Sla aol tvza
hizlua- tpuklk vm tlu il wsbunlk pu opz kllwlza ylclyplz - zahuk aoha thu vu opz slnz, zla opz mlla h-nvpun, huk
ol dpss pumhsspisf slhk fvb av dhaly, pm dhaly aolyl il pu hss aoha ylnpvu. Zovbsk fvb lcly il haopyza pu aol nylha
Htlypjhu klzlya, ayf aopz lewlyptlua, pm fvby jhyhchu ohwwlu av il zbwwsplk dpao h tlahwofzpjhs wyvmlzzvy. Flz, hz
lclyf vul ruvdz, tlkpahapvu huk dhaly hyl dlkklk mvy lcly. 
Iba olyl pz hu hyapza. Ol klzpylz av whpua fvb aol kylhtplza, zohkplza, xbplalza, tvza lujohuapun ipa vm yvthuapj
shukzjhwl pu hss aol chsslf vm aol Zhjv. Doha pz aol joplm lsltlua ol ltwsvfz? Aolyl zahuk opz ayllz, lhjo dpao h
ovssvd aybur, hz pm h olytpa huk h jybjpmpe dlyl dpaopu; huk olyl zsllwz opz tlhkvd, huk aolyl zsllw opz jhaasl;
huk bw myvt fvukly jvaahnl nvlz h zsllwf ztvrl. Kllw puav kpzahua dvvkshukz dpukz h thgf dhf, ylhjopun av vclyshwwpun
zwbyz vm tvbuahpuz ihaolk pu aolpy opss-zpkl isbl. Iba aovbno aol wpjabyl splz aobz ayhujlk, huk aovbno aopz
wpul-ayll zohrlz kvdu paz zpnoz sprl slhclz bwvu aopz zolwolyk'z olhk, fla hss dlyl chpu, buslzz aol zolwolyk'z lfl
dlyl mpelk bwvu aol thnpj zaylht ilmvyl opt. Nv cpzpa aol Wyhpyplz pu Qbul, dolu mvy zjvylz vu zjvylz vm tpslz fvb
dhkl rull-kllw htvun Apnly- spsplz - doha pz aol vul johyt dhuapun? - Dhaly - aolyl pz uva h kyvw vm dhaly aolyl!
Dlyl Uphnhyh iba h jhahyhja vm zhuk, dvbsk fvb ayhcls fvby aovbzhuk tpslz av zll pa? Dof kpk aol wvvy wvla vm
Aluulzzll, bwvu zbkklusf yljlpcpun adv ohukmbsz vm zpscly, klspilyhal dolaoly av ibf opt h jvha, dopjo ol zhksf
ullklk, vy puclza opz tvulf pu h wlklzayphu aypw av Yvjrhdhf Ilhjo? Dof pz hstvza lclyf yvibza olhsaof ivf dpao h
yvibza olhsaof zvbs pu opt, ha zvtl aptl vy vaoly jyhgf av nv av zlh? Dof bwvu fvby mpyza cvfhnl hz h whzzlunly,
kpk fvb fvbyzlsm mlls zbjo h tfzapjhs cpiyhapvu, dolu mpyza avsk aoha fvb huk fvby zopw dlyl uvd vba vm zpnoa vm
shuk? Dof kpk aol vsk Wlyzphuz ovsk aol zlh ovsf? Dof kpk aol Nyllrz npcl pa h zlwhyhal klpaf, huk vdu iyvaoly vm
Qvcl? Zbylsf hss aopz pz uva dpaovba tlhupun. Huk zapss kllwly aol tlhupun vm aoha zavyf vm Uhyjpzzbz, dov iljhbzl
ol jvbsk uva nyhzw aol avytluapun, tpsk pthnl ol zhd pu aol mvbuahpu, wsbunlk puav pa huk dhz kyvdulk. Iba aoha zhtl
pthnl, dl vbyzlsclz zll pu hss ypclyz huk vjlhuz. Pa pz aol pthnl vm aol bunyhzwhisl wohuavt vm spml;
huk aopz pz aol rlf av pa hss.
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by BobS0327
0

Hey forum, I'm very new to C++, and I need some help. I'm currently having trouble trying to write a Caesar cipher in C++. So far, I'm able to get it to read characters from a text file, and let it count the # each letter appears in an array. What I'm trying to do exactly is, find the most frequent letter, and then use that to find the shift. But before that, i want to display which letter that it was that was most frequent, and the number of times it was in the text.

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

using namespace std;

//Globals
const int ARRAY_SIZE = 128;
const int E_LOC = 4;
int list[ARRAY_SIZE] = {0};
string reply;
ofstream outputFileName;
ifstream inputFile;
char ch;
	
//Prototypes
void characterCount(char ch, int list[]); 
void calcShift( int& shift, int list[]); 
void writeOutput(ifstream &in, ofstream &out, int shift);

int main() {
	int shift = 0;
    string inputFileName;
    char ch;
	 
	//Input File Name
    cout << "Input file name: ";
    getline(cin, inputFileName);

    // Open the input file.
    inputFile.open(inputFileName.c_str());
    
	// Check the file opened successfully.
	if ( ! inputFile.is_open()) {
        cout << "Unable to open input file." << endl;
        cout << "Press enter to continue...";
        getline(cin, reply);
        exit(1);
    }
	//Open the output file.
	cout << "Output file name: ";
	getline(cin, inputFileName);
	outputFileName.open(inputFileName.c_str());

    // This section reads and echo's the file one character (byte) at a time.
    while (inputFile.peek() != EOF) 
	{
        inputFile.get(ch);
		characterCount(ch, list);
    }

	calcShift(shift, list);

	inputFile.clear();
    inputFile.seekg(0);

	inputFile.close();
	inputFile.open(inputFileName.c_str());   

	cout << endl;
	system("PAUSE");
		return 0;
}

void characterCount(char ch, int list[])
{
		if (ch >= 'a' && ch <= 'z')
		{
			int asciiCode = 0;
 
			asciiCode = (static_cast<int>(ch)); 
			list[asciiCode]++; 
			cout << ch;
		}
}


void calcShift( int& shift, int list[])
{
	int mostUsedLetter = list[0];
	int mUsedIndex;

//Loop to search for greatest count and location (index) of greatest count.
for(int i = 0; i < ARRAY_SIZE; i++) 
	{
		if(list[i] > mostUsedLetter)
		{
			mostUsedLetter = list[i];
			mUsedIndex = i;
			cout << endl << "The letter: " << static_cast<char>(mUsedIndex) << endl << " was shown " << mostUsedLetter << " times." << endl;
			//shift = mUsedIndex - E_LOC;
		}
		
	}
}

I just threw up a bunch of default headers first, and worked from there. This code isn't complete yet, but I'm going step by step and trying to fix my mistakes before i continue. The current problem I'm having at the moment is that when i execute the code, it displays all the characters normally, but at the point where i prompt it to display the most frequent letter, it displays the message twice. One for a and l. Why is it doing that? Feel free to flame me with feedback, I really need it. Advice on waht to do next would be nice too.

The mostUsedLetter variable is initially set to ZERO. The list storage area for the letter 'a' will almost always have a count greater than ZERO. Thus, 'a' will be listed since it's count is greater than ZERO. But it doesn't necessarily mean that 'a' is the most used letter. It just means that the count of 'a' is greater than ZERO.

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.