File output with a switch statement

I am working on a project where I have to take a string from a file (in1.txt) and then run it through this function (called 'code') and then output the 'codded' result to an output file (out1.txt). Everything is working great; in the in1.txt I wrote "hobbit"; it takes it in converts it then... instead of outputting the codded result ( Hobbit Orc Bilbo Bilbo Isildur Thorin) it just output "hobbit" same as the in1.txt

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
#include <iostream>  //declaring variables
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string code(string& line);
int main()
{
    ofstream outf;
    ifstream myfile;
    string infile;
    string line;
    string outfile;

    cout << "Please enter an input file.(in1.txt)" << endl;
    cin >> infile;   //prompts user for input file

	if (infile == "in1.txt"){      //read whats in it and write to screen
		myfile.open("in1.txt");cout << endl;
		getline (myfile, line);
		cout << line << endl;
		}
	else{ cout << "Unable to open file." << endl;}

        code(line);  //calls the function
	cout << endl << endl; //to make it look nice...er

	outf.open("out1.txt"); //opens output file
        cout << "Please enter an output file.(out1.txt)" << endl;
	cin >> outfile;
	if (outfile == "out1.txt"){   //outputs "the word's... then 'line'
	outf << "The word's translation is: " << line;}//line should be the coded stuff
	else{ cout << "Unable to open file." << endl;}
	myfile.close();
	outf.close(); //closes both files

	return 0;
}

string code(string& line)
{
	

	for(int i = 0; i<line.size(); i++)
	switch (toupper(line[i]))
	{
        case 'A': cout << "Aragorn "; break;
		case 'B': cout << "Bilbo "; break;
		case 'C': cout << "Celeborn "; break;
		case 'D':  cout << "Durin "; break;
		case 'E':  cout << "Eagle "; break;
		case 'F':  cout << "Frodo "; break;
		case 'G':  cout << "Gwaihir "; break;
		case 'H':  cout << "Hobbit "; break;
		case 'I':  cout << "Isildur "; break;
		case 'J':  cout << "Justice "; break;
		case 'K':  cout << "Kondor "; break;
		case 'L':  cout << "Legolas "; break;
		case 'M':  cout << "Mithrandir "; break;
		case 'N':  cout << "Noldor "; break;
		case 'O':  cout << "Orc "; break;
		case 'P':  cout << "Pippin "; break;
		case 'Q':  cout << "Quickbeam "; break;
		case 'R':  cout << "Rohan "; break;
		case 'S':  cout << "Saruman "; break;
		case 'T':  cout << "Thorin "; break;
		case 'U':  cout << "Ungoliant "; break;
		case 'V':  cout << "Valar "; break;
		case 'W':  cout << "Warg "; break;
		case 'X':  cout << "X-Garoth "; break;
		case 'Y':  cout << "Yrch "; break;
		case 'Z':  cout << "Zirak "; break;
		case ' ': cout << " " << endl; break;
        default: cout <<"That is not a correct input." << endl; break;
	}
	return line;

}


Now my idea on how to solve this is just include a write to file bit in each case in the function. My friend said that I'd have to pass the file output info into the function parameters using '&'. I believe this will work I'm just trumped.

Thanks as always!
~Shadow
Topic archived. No new replies allowed.