Caesar Cipher, yes homework


I'll start simple how do I post in here so it shows my code lines?
By adding code tags:
[code]code here[/code]
my problem is between lines 116 and 130 (specifically with line 127)

I am trying to create a function that returns the most counted letter so I can create other functions too factor in the shift of the cipher. currently the program gives an error making me think the array is going out of bounds. if I put line 127 outside the if statement it then returns a null value.

any help is appreciated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include  <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;



void initialize (int& lc, int list[]);

void copyText (ifstream& intext, ofstream& outtext, char ch, int list []);

void characterCount ( char ch, int list[]);

void writeTotal (ofstream& outtext, int lc, int list[]);

int largestLetter (int list[]); // takes the list INDEX and returns the most often counted letter
int main ()
{
	int lineCount;
	int letterCount [26];
	char ch;
	char LARGEST;
	string inputFileName;

	ifstream infile;
	ofstream outfile;
	
	cout << "please enter the file name";
	getline (cin, inputFileName);
	infile.open (inputFileName);
	
	if (!infile)
	{
	cout << "cannot open the file" << endl;
	return 1;

	}
	
	outfile.open ("encryptedout.txt");
	
	initialize(lineCount, letterCount);

	infile.get(ch);

	while (infile)
	{
		copyText (infile, outfile, ch, letterCount);
		lineCount ++;
		infile.get(ch);
		
	}

	cout << endl << endl<< "was the encrypted text"<< endl;
	


	writeTotal(outfile, lineCount, letterCount);

	LARGEST = largestLetter(letterCount);
	
	cout <<"the largest count was " << LARGEST;
	infile.close();
	outfile.close();

return 0;

}


void initialize (int& lc, int list [])
{
	int j;
	lc = 0;
	for (j=0; j<26; j++)
		list[j]= 0;
}

void copyText (ifstream& intext, ofstream& outtext, char ch, int list [])
{
	while (ch != '\n')
	{
		outtext<< ch;

		characterCount (ch, list);
		intext.get(ch);
		cout << ch; // echos out the encrypted text
	}
	outtext<<ch;
	
}

void characterCount (char ch, int list [])
{
	int index;
	ch = toupper(ch);
	
	index = static_cast<int>(ch) -static_cast <int> ('A');
	if (0 <=index && index <26)
		list[index]++;
}

 void writeTotal (ofstream& outtext, int lc, int list[])
 {
	 int index;

	 outtext<< endl<< endl;
	 outtext << "The number of lines = " << lc << endl;
	for (index = 0; index < 26; index++)
		 outtext << static_cast<char>(index +static_cast<int>('A')) << " count " << list[index] << endl;

 }

 
int largestLetter (int list[])
{
	int index = 1;
	int maxIndex =0;
	int largest= 0;

		for (index =1; index <26; index++)
		{
			if (list[index] > list[maxIndex]);
			maxIndex = list[index];
		
			largest = list[maxIndex];
		}
	return largest;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int largestLetter (int list[])
{
	int index = 1;
	int maxIndex =0;
	int largest= 0;

		for (index =1; index <26; index++)
		{
			if (list[index] > list[maxIndex]);//this if statment doesnt do anything
			maxIndex = list[index];//<-- may i ask what your trying to do here?
			largest = list[maxIndex];//maxindex here is the amount of charactors at list[index]
		}
	return largest;
}


what are you trying to do there?

edit: tried to be more constructive...i also tryed to fix that function
fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int largestLetter (int list[])
{
	int index = 1;
	int maxIndex =0;
	int largest= 0;

		for (index =0; index <26; index++)//fixed here too
		{
			if (list[index] > list[maxIndex])
			      maxIndex = index;//this should make it work..
			largest = list[maxIndex];
                }
	return largest;
}


I would do it like this:
1
2
3
4
5
6
7
8
9
10
11
int largestLetter (int list[])
{
	int largest= 0;

		for (int index =0; index <26; index++)
		{
			if (list[index] > largest)
			     largest=list[index];
                }
	return largest;
}
Last edited on
Topic archived. No new replies allowed.