Translator

Nov 11, 2015 at 1:04am
Hey guys, I am new to programming and have to write a translation program based off of words that were given to us in two text files, an English and Spanish one. Whenever I run the program I have so far it keeps crashing though, any advice?


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

using namespace std;
    void display();
    const string space = "                     ";
    const string space2 = "                                   ";
    const int length = 38;
    ifstream inFile;
    ofstream outFile;
    fstream english("ENG.txt");
    fstream spanish("SPAN.txt");


int main()
{
    string word, ENG[length], SPAN[length], save;
    int num, lang;
    display ();
    for(num=0; num<=length; num++)
            {
                getline(english,save);
                ENG[num]=save;
            }
    cin >> lang;
    system("cls");

    switch(lang)
    {
        case 1:
            cout << "What word would you like to translate?" << endl;
            cin >> word;
            break;

    }
    return 0;
}

void display()
{
    cout << space << "What would you like to translate to?\n" << endl;
    cout << space2 << "1. Spanish" << endl;
    cout << space2 << "2. English" << endl;
    cout << space2 << "0. Exit" << endl;
}
Last edited on Nov 11, 2015 at 1:35am
Nov 11, 2015 at 1:21am
first, What is in each Text file?
second, Please use code tags!
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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
void display();
const string space = " ";
const string space2 = " ";
const int length = 38;
ifstream inFile;
ofstream outFile;
fstream english("ENG.txt");
fstream spanish("SPAN.txt");


int main()
{
string word, ENG[length], SPAN[length], save;
int num, lang;
display ();
for(num=0; num<=length; num++)
{
getline(english,save);
ENG[num]=save;
}
cin >> lang;
system("cls");

switch(lang)
{
case 1:
cout << "What word would you like to translate?" << endl;
cin >> word;
break;

}
return 0;
}

void display()
{
cout << space << "What would you like to translate to?\n" << endl;
cout << space2 << "1. Spanish" << endl;
cout << space2 << "2. English" << endl;
cout << space2 << "0. Exit" << endl;
}
Nov 11, 2015 at 1:23am
sorry im not sure how to use code tags, just created my account D:
in each file is a list of 38 words with matching english words in the ENG.txt and the corresponding spanish words in the same order in the SPAN.txt file
Nov 11, 2015 at 1:31am
What is the file format? Is there something separating each word? What do the text files look like?
As for code tags...
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on Nov 11, 2015 at 1:32am
Nov 11, 2015 at 1:34am
the text files looks like this

BLACK
BLUE
BROWN
GREEN
GOLD
GREY
INDIGO
ORANGE
PINK
PURPLE
RED
SILVER
TURQUOISE
VIOLET
WHITE
YELLOW
ONE
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
TWENTY
HUNDRED
HELLO
GOODBYE
GOOD
JUNGLE
PARK
SOCCER
BASEBALL
HOCKEY
BASKETBALL
FINISHED

(and then the spanish text file is the same format but in spanish)
Nov 11, 2015 at 1:44am
I am running the program in my computer just fine. I have no issues what so ever. What compiler are you using?
Nov 11, 2015 at 1:52am
oh, it is not running the program that I am having difficulty with, it's getting the program to take the word the user inputs and convert it to the opposite language
Nov 11, 2015 at 1:54am
closed account (48T7M4Gy)
Runs fine here on the shell too.

My observation is you have no code to read in the txt file data. What you have at the moment is a (successful) menu display.

Try this tutorial and the demo then extend it to load up your arrays.
http://www.cplusplus.com/doc/tutorial/files/

First step is just make up a new small program to read in an english.txt (or the other one) file and print out the words. Take it in easy steps.
Nov 11, 2015 at 1:54am
oh, ok. You should reword your first post then.
Nov 11, 2015 at 1:59am
I recommend looking at this post to help you out. Translators are only easy if you know what your doing. http://wiki.tcl.tk/10262
It's an added challenge that you are trying to full the words from a text file aswell.
Nov 11, 2015 at 2:00am
closed account (48T7M4Gy)
Sheeesh!
Nov 11, 2015 at 3:33am
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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
    void display();
    const string space = "                     ";
    const string space2 = "                                   ";
    const int length = 38;
    ifstream inFile;
    ofstream outFile;
    ifstream english("ENG.txt");
    ifstream spanish("SPAN.txt");


int main()
{
    string word, ENG[length], SPAN[length], save;
    int num, lang;
    display ();
    cin >> lang;
    system("cls");
    for(num=0; num<=length; num++)
            {
                getline(english,save);
                ENG[num]=save;
                getline(spanish,save);
                SPAN[num]=save;

            }



    switch(lang)
    {
        case 1:
            cout << "What word would you like to translate?" << endl;
            cin >> word;
            break;

        case 2:
            cout << "What word would you like to translate?" << endl;
            cin >> word;
            break;

    }
    return 0;
}

void display()
{
    cout << space << "What would you like to translate to?\n" << endl;
    cout << space2 << "1. Spanish" << endl;
    cout << space2 << "2. English" << endl;
    cout << space2 << "0. Exit" << endl;

}
[code]


hmm... it is this code that keeps crashing
Last edited on Nov 11, 2015 at 3:33am
Nov 11, 2015 at 4:05am
closed account (48T7M4Gy)
First step: What did you do EXACTLY to make it crash. What did you type and what did you get as output? Why do you say it crashed, what did you expect the output to be?

Second step: What is your code designed to do? Where is your pseudocode?

What is the code between lines 23 to 30 supposed to do? Just in simple pseudocode or 1 or 2 sentences in plain english will do.

Also please explain what the difference is between your two versions that makes them substantially different?
Last edited on Nov 11, 2015 at 4:07am
Nov 11, 2015 at 4:30am
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

int display();

int main()
{
	string word;
	string ENG[] = {"BLACK","BLUE", "BROWN","GREEN"};
	string SPAN[] = {"NERO","AZUL","MARRON","VERDE"};

	int length = sizeof(ENG) / sizeof(string);

	int lang = display();

	switch (lang)
	{
	case 0:
		exit(0);
		break;
	case 1:
		cout << "What word would you like to translate?" << endl;
		cin >> word;
		for (int i = 0; i < length; i++)
		{
			if (word == ENG[i])
				cout << SPAN[i] << endl;
		}
		break;

	case 2:
		cout << "What word would you like to translate?" << endl;
		cin >> word;
		break;

	default:
		cout << "Error stuff" << endl;
		// sort this out later
		
	}

	system("pause");
	return 0;
}

int display()
{
	int selection = 0;

	cout << "What would you like to translate to?\n" << endl;
	cout << "1. Spanish" << endl;
	cout << "2. English" << endl;
	cout << "0. Exit" << endl;

	cin >> selection;

	return selection;
}

Better late than never ...
Nov 11, 2015 at 10:42pm
i can see that coe but what you have done isn't what i think that they are trying to accomplish. You are using an array rather than pulling the words from a file. I'm working on efficient code that will pull the text from a file. It's mostly aesthetics for now though.
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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
void display();
void Language();
string EngToSpan();
string SpanToEng();
const string space = " ";
const string space2 = " ";
const int length = 38, one=1;
string word, ENG[length], SPAN[length], save;
int num, lang;
char YorN;
ifstream inFile;
ofstream outFile;
fstream english("ENG.txt");
fstream spanish("SPAN.txt");

int main()
{
while(one==1){
display ();
Language();}
return 0;
}

void display()
{
cout << space << "What language would you like to translate to?\n" << endl;
cout << space2 << "1. Spanish" << endl;
cout << space2 << "2. English" << endl;
cout << space2 << "0. Exit" << endl;
cin>>num;
return;
}

void Language()
{
    if(num==1)
    {
    cout<<"\nYou have chosen English to Spanish.\n";
    EngToSpan();
    }else if(num==2)
    {

    }else if(num==0)
    {
    system("exit");
    }else
    {
    cout<<"\n\nSorry You can't use that Number for selection yet. Please Try Again.";\
    system("CLS"); display();
    }
return;
}
string EngToSpan()
{

}

string SpanToEng()
{

}
Last edited on Nov 11, 2015 at 11:25pm
Nov 11, 2015 at 10:52pm
for(num=0; num<=length; num++)

You are exceeding the bounds of your array.
Nov 11, 2015 at 10:55pm
closed account (48T7M4Gy)
@jasoncoe
Exactly! You now have to find a way to 'feed' the arrays by delving into the two text files and extracting their contents in an orderly way. But that aspect was talked about many posts ago.

Put the two together and your new beaut translator will sing.
Last edited on Nov 11, 2015 at 11:52pm
Topic archived. No new replies allowed.