# Help with Converting between letters and numbers using arrays...

• 04-18-2005
MyglyMP2
Help with Converting between letters and numbers using arrays...
I think i've got it but not too sure, and If anyone knows any other way to do this how could i do it?
Code:

```#include<iostream> #include<cctype> #include<string> #include<fstream> #include<iomanip> using namespace std;   char integer_to_letter(int); int letter_to_integer(char); void print_header(); int main(int argc, char *argv[]) {   const int SIZE = 26;   int freq[SIZE] = {0};   char letter;   int index;   ifstream fin(argv[1]);   ofstream fout(argv[2]);   print_header();   fin >> letter;   while(!fin.eof())     {     if(isalpha(letter))       {       index = letter_to_integer(tolower(letter));       freq[index]++;       }     fin >> letter;     }   fout << "Count the occurances of letters in a file" << endl         << "producing a frequency" << endl << endl         << "letter freq          letter freq" << endl         << "------ -------------- ------ ---------------" << endl;   for(int i = 0; i < 13; i++)     {     char one = integer_to_letter(i);     tolower(one);     char two = integer_to_letter(i+13);     tolower(two);     fout << right << setw(6) << one << left << " "           << setw(15) << freq[i] << " "           << right << setw(6) << two << " "           << left << freq[(i+13)] << endl;     }   fin.close();   fout.close();   return 0; } /*  * Name: letter_to_integer  * Purpose: Convert a letter to the corresponding number  * Receive: Data from a file  * Return: Number corresponding to the letter  */ int letter_to_integer(char letter) {   int index = (letter - 'a');   return index; } /*  * Name: integer_to_letter  * Purpose: Convert a number to the corresponding letter  * Receive: Data from a file  * Return: Letter corresponding to the number  */ char integer_to_letter(int index) {   return(static_cast<char>('a' + index)); } /*  * Name: print_header  * Purpose: Display header  * Receive: Nothing  * Return: Header  */ void print_header() {   cout << "\n";   cout << "\n";   cout << "\n"; }```
• 04-18-2005
7stud
Code:

```for(int i = 0; i < 13; i++) {         char one = integer_to_letter(i);         tolower(one);         char two = integer_to_letter(i+13);         tolower(two);```
What do those lines do?
• 04-19-2005
treenef
I assume you want to do this so that you may order your words in alphabetical order. However, you can do this using the STL which is easier.

If this is not what you want to do then ignore this post.
• 04-19-2005
Welshy
Quote:

Originally Posted by 7stud
Code:

```for(int i = 0; i < 13; i++) {         char one = integer_to_letter(i);         tolower(one);         char two = integer_to_letter(i+13);         tolower(two);```
What do those lines do?

Click me!
• 04-19-2005
7stud
Quote:

Explanation: Tolower will return the ASCII value for the lowercase equivalent of the ASCII character chr, if it is a letter.
...and the question still stands: what do those lines do? Try this Welshy:
Code:

```char ch = 'A'; tolower(ch); cout<<ch<<endl;```
and you'll see what I mean. ;)