Array with output based on user input.

I'm trying to make a single dimensional array that pulls its contents from a file as well an input string from a user. It needs to take the word, break that word into letters, and correlate each letter with its corresponding array designation. (I hope that made sense.)
As an example, if I input the word "cat" it needs to output "Charlie Alpha Tango". My array has 26 components labeled A-Z. My problem is that whenever I input a word, no matter what it is, I get "Zulu Uniform Lima". I can't make the user input correspond with its correct letter in the array. I've checked around on this site and others and can't find a solution.
Here's my code:

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
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

int stop;
int index;

int main()
{
	string word;
	string UserInput;
	ifstream inFile;
	ofstream outFile;
	outFile.open("C://Users/Eli/Documents/Visual Studio 2010/ICAO Output.txt");
	inFile.open("C://Users/Eli/Documents/Visual Studio 2010/ICAO.txt");
	string letters[26];
	int limit;
	int counter;

	cout << "Please enter a word you would like translated into the ICAO alphabet." << endl;
	cin >> UserInput;
	outFile << UserInput;
	
	if (!inFile)
		cout << "File did not open!" << endl;

	
	
	inFile >> word;

	while (inFile)
	{
		letters[index] = word;
		inFile >> word;
		index++;
	}
	
	for (counter = 0; counter < 26; counter++)
	{	
		cout << (char) (counter + 'A') << ": " << letters[counter] << endl;
		outFile << (char) (counter + 'A') << ": " << letters[counter] << endl;
	}

	cout << endl << "Please enter a word." << endl;
	cin >> UserInput;
	limit = UserInput.length();
	

	for (counter = 0; counter < limit; counter++)
	{
		index = toupper(word.at(counter)) - 'A';
		cout << letters[index] << " ";
		outFile << letters[index] << " ";
	}

	inFile.close();
	outFile.close();


	cin >> stop;
	return 0;
}
One thing is that index is left uninitialized. Should start from 0.
Thanks for the suggestion. That brings me one step closer, however I am still having the same problem.
Last edited on
1
2
3
4
5
6
7
8
9
10
cout << endl << "Please enter a word." << endl;
cin >> UserInput;
limit = UserInput.length();

for (counter = 0; counter < limit; counter++)
{
	index = toupper(word.at(counter)) - 'A';     
	cout << letters[index] << " ";
	outFile << letters[index] << " ";
}
Please enter a word.
Dupa
Zulu Uniform Lima Uniform

1
2
3
4
5
6
7
8
9
10
cout << endl << "Please enter a word." << endl;
cin >> UserInput;
limit = UserInput.length();

for (counter = 0; counter < limit; counter++)
{
	index = toupper(UserInput.at(counter)) - 'A';
	cout << letters[index] << " ";
	outFile << letters[index] << " ";
}
Please enter a word.
Dupa
Delta Uniform Papa Alpha
Last edited on
Thank you so much JockX! If I could bake you a pan of cookies and send them to you digitally, I would. I've been trying to solve this for almost a week now. You're amazing!
Topic archived. No new replies allowed.