Memorization Program

I am trying to make a program that takes file input from something in Latin "EcceGratum.txt" and then prompts the user for English (in this case), comparing it to that line of "EcceGratumEnglish.txt" storing both of these values in respective arrays. I then use a while loop with if and else inside of it, to increment only if the strings match, and if they don't, merely display the english, and then prompt the user to input it again after clearing the screen. Unfortunately, I keep getting a "correct" response when it's not. A few other problems too.

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

using namespace std;

int main()
{
	ifstream myfile("EcceGratum.txt"), myfile2("EcceGratumEnglish.txt");
	string line, array[50], array2[50];
	int a = 0;
	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			array[a] = line;
			a++;
		}
	}
	
	a = 0;

	if (myfile.is_open())
	{
		while (getline(myfile2, line))
		{
			array2[a] = line;
			a++;
		}
	}

	string input;

	int i = 0;

	while(i != 30)
	{
		cout << array[i] << endl;
		cin >> input;
		if ((array2[i] == input)){
			cout << "correct" << endl;
			++i;
		}
		else {
			cout << array2[i] << endl;
			system("PAUSE");
			system("CLS");
		}
	}

	cin.get();

	return 0;
}


It's the string matching and the if that's giving me the most problems. I think.
Your code works OK for me. (As I don't know Latin I used French.)

Andy

(tweaked code so it didn't clear screen to make flow clearer for checking.)

un
one
correct
deux
three
wrong
two
try again!
deux
four
wrong
two
try again!
deux
two
correct
trois
three
correct
quatre
six
wrong
four
try again!
quatre
seven
wrong
four
try again!
quatre
four
correct
cinq
five
correct


Tweaked 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
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm> // Edit: needed for std::max

using namespace std;

int main()
{
	ifstream myfile("EcceGratum.txt"), myfile2("EcceGratumEnglish.txt");
	string line, array1[50], array2[50];
	int a1 = 0;
	int a2 = 0;

	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			array1[a1] = line;
			a1++;
		}
	}
	
	if (myfile.is_open())
	{
		while (getline(myfile2, line))
		{
			array2[a2] = line;
			a2++;
		}
	}

	int a = max(a1,a2);

	string input;

	int i = 0;

	while(i != a)
	{
		cout << array1[i] << endl;
		cin >> input;
		if ((array2[i] == input)){
			cout << "correct" << endl;
			++i;
		}
		else {
			cout << "wrong" << endl;
			cout << array2[i] << endl;
			cout << "try again!" << endl;
			//system("PAUSE");
			//system("CLS");
		}
	}

	cin.get();

	return 0;
}


EcceGratum.txt

un
deux
trois
quatre
cinq


one
two
three
four
five

Last edited on
I get an error saying, Error: identifier "max" is undefined. I'm using Microsoft Visual C++ Express. I have no idea why the program doesn't want to read string. Does it go until the null character? Maybe the input file doesn't have a null character at the end of every line. I suppose I could enter one.
To solve the max() problem add

#include <algorithm>

It must have been indirectly included when I built with the GCC( compiler (I've edited my earlier post accordingly.)

Maybe the input file doesn't have a null character at the end of every line. I suppose I could enter one.

It doesn't have or need any null characters in the file. nulls are only needed when their stored in memory (so you can find the end of the string.) So don't try to add any.

Andy
Last edited on
Topic archived. No new replies allowed.