Pull from arrays and output

Hello, I have been spinning wheels for hours now. I have a great foundation for my solution but I do not know how to enter in the language and then output from the array.

Any suggestions would be appreciated.
Thank you
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



#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
void openDataFile();
void variable (int n);
void loadArrays ();
string english;
string another;
string inputPathName[80];
fstream inFile;
string EnglishArray [10];
string anotherArray [10];
ifstream dataIn;
int input;
int n;
int i;
int main ()
{
	openDataFile ();
	loadArrays ();
	variable (n);


	//	char reply;
	//	cout << "press q and enter to quit: ";
	//	cin >> reply;
}
void openDataFile() {
	do {
		cout << "Please enter input path name: ";
		cin >> inputPathName;
		dataIn.open(inputPathName);
		if (!dataIn.is_open()) {
			cout << "File input failed" << endl;
		}
	} while (!dataIn.is_open());
	cout << "File Opened Successfully" << endl;
}


void another2English() {
	cout << "French: ";
	cin >> french;

}

void English2another() {
	cout << "English: " << endl;
	cin >> english;
}

void select123 (int n) {
	do {
		cout << "Enter 1 to translate another to English" << endl;
		cout << "Enter 2 to translate English to another" << endl;
		cout << "Enter 3 to Quit" << endl;
		cin >> n;

	

void loadArrays() {	
	int i=0;
	while (dataIn >> EnglishArray[i] >> anotherArray[i]) {			
		i = i + 1;
	}
	//	for (i=0; i<10; i++)
	//		cout << i << "  " << EnglishArray[i] << "  " << anotherArray[i] << endl;
}
Last edited on
And it doesnt like when i string inputPathName. It only works when I use char inputPathName
Do you have sample data?

Try this on for size and look at hints @ 40 & 48

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

using namespace std;

    string  Words [10][2];
    
int LoadStrings ( ifstream &StringData ) {
    int Pntr = 0;
    
    while ( StringData.good () )
        StringData >> Words [Pntr][0] >> Words [Pntr++][1];
    
    cout << Pntr << " Sets\n";    
    return Pntr;
    }
    
int main ( int ArgV, char **ArgC ) {

    ifstream DataIn;
    int ArraySize, Answer;    
    bool CarryOn = true;
    
    cout << "\n\nLanguage translation\n" << endl;
    
    if ( ArgV == 2 ) {
        DataIn.open ( ArgC [1] );
        
        if ( DataIn.is_open () ) {
            if ( ArraySize = LoadStrings ( DataIn ) ) {
                while ( CarryOn ) {
                    cout << "\t[1] French -> English\n";
                    cout << "\t[2] English -> French\n";
                    cout << "\t[3] Let me OUT!!\n";
                    
                    cin >> Answer;
                    
                    switch ( Answer ) {
                        // Hint: this can be done with a toggle
                        case 1:
                        case 2:
                            break;
                        
                        case 3:
                            CarryOn = false;
                        }
                    // Code to translate will go here
                    }                  
                }
            else
                cout << "Problem loading array of words\n";
            }
        else
            cout << "Couldn't open\n" << ArgC [1] << "\n";
        }
    else
        cout << "Please specify file in command line\n" << endl;
        
    cout << "Done\n\n";
    return 0;
    }
The premise is taking a list of 10 english and comparing them against french words, there are 10 of each. When my translation of the words works. what I cannot accomplish getting out of my while loop. It reads out my translation, but then it sits there waiting for me to put in another word. When I type in the french word it translates to English again, but still waiting for me to enter another word to translate. how can I easily get out of this loop so I may get back out to a choice input screen. I am reading in the words like this:

i je
you vous
am suis
are etes
happy heureux
sad triste
hot chaud
cold froid
but mais

This is the for while loop I cannot get out of:

1
2
3
4
5
6
while(cin.peek() != 0) {
		cin >> french;
		for (i = 0; i < 10; i++) 
			if (!strcmp(french, FrenchArray[i])) 
				cout << " " << EnglishArray[i];
	}


How do I get out without testing for cin >> french
get.line??
I know peek is incorrect
Thank you
I believe if you change your condition to cin.peek () != '\n' it will work.

Assuming french is a c-string... you should probably be using getline so you can indicate the length of the buffer.

1
2
3
4
5
6
    while (cin.peek() != '\n' && cin >> french )
    {
        for (i = 0; i < 10; i++) 
            if (!strcmp(french, FrenchArray[i])) 
                cout << " " << EnglishArray[i];
    }


Edit:

This is probably a better solution.

1
2
3
4
5
6
7
8
9
10
11
12
#include <cctype>    

    // skip any whitespace from previous
    // formatted extraction operations.
    while ( isspace(cin.peek()) )
        cin.ignore() ;

    cin.getline(french, size_of_french);

    for (i = 0; i < 10; i++) 
        if (!strcmp(french, FrenchArray[i])) 
            cout << " " << EnglishArray[i];



Last edited on
I have a working program. But for some reason I cannot exit properly. I keep getting warning codes. I know i'm just missing something easy from working on this for so long.
These are the error codes i'm getting:

Unhandled exception at 0x104cee04 in languageTranslator.exe: 0xC0000005: Access violation writing location 0x108e6bf0.

There is no source code available for the current location.

As well as an End Now prompt for my solution.

I have both ways I have tried to get out. the comments out was other option I thought would work. This is the end of my do-if-else.
Thank you again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
else 
		cout << "Thank you for using my translation! " << endl;
		string reply;
		cout << "Press q to quit: ";
		cin >> reply;

	} while (n==1 || n==2);
}

void loadArrays() {	
	int i = 0;
	while (dataIn >> EnglishArray[i] >> FrenchArray[i]) {			
		i = i + 1;
	}
}
//void endProgram() {
//	string reply;
//	cout << "Press q to quit: ";
//	cin >> reply;
//} 
Last edited on
Topic archived. No new replies allowed.