Project, some help !

Hello.
Basicaly,i have to do a c++ project thats read some instruction from a file, then compare them to some line from another file.

i have 2 file's:
1. "instr.txt" , that contains:
1
2
  mov ax,03
  add ax,05


2."mne.txt",that contains:
1
2
3
4
  add
  sub
  mul
  mov


Now, i have to read each line from the "instr.txt" to separate strings. Then read each line from "mne.txt" too.
Then i have to compare the first 3 characters from strings that store line 1 from "instr.txt" to each line from "mne.txt". If i found the first 3 characters from line 1 from "instr.txt" ( mov) , i need to move forward and check for register (ax, and store him) , then move forward and check the number (03, witch i need to move it to ax).

Note1:
add means +
sub means -
mul means *
mov means "copy" or move.



Note2:
 
   mov ax,03

Means move to var ax 03 (ax = 3;)

 
   add ax,05

Means ax+5 = 3+5 (from above) = 8.



Thanks!
What have you tried so far? You didn't really tell us what you are having trouble with.
I manage to read each line from both files and put them in separate strings, but i can't find how to compare "first line string" from "instr.txt " [i need first to compare first 3 characters from string]with all 4 strings from "mne.txt", and then move forward and compare the next characters from "fisrt line string" from "instr.txt" with all 4 strings from second file.

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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;



//Read instr file
string* citire_fisier_instr()
{
    ifstream f_intrare;
	string linie, *total_linii;
	int contor_linie = 0;
	f_intrare.open("instr.txt");
	while (getline(f_intrare, linie))
		contor_linie++;
	total_linii = new string[contor_linie];
	int i = 0;
	f_intrare.close();
	f_intrare.open("instr.txt");
	while (getline(f_intrare, linie))
	{
		total_linii[i] = linie;
		i++;
	}

	for (i = 0; i < contor_linie; i++)
	cout << total_linii[i] << endl;


}

//Read mne file
string* citire_fisier_mne()
{
    ifstream f_intrareM;
	string linieM, *total_liniiM;
	int contor_linieM = 0;
	f_intrareM.open("mne.txt");
	while (getline(f_intrareM, linieM))
		contor_linieM++;
	total_liniiM = new string[contor_linieM];
	int k = 0;
	f_intrareM.close();
	f_intrareM.open("mne.txt");
	while (getline(f_intrareM, linieM))
	{
		total_liniiM[k] = linieM;
		k++;
	}

	for (k = 0; k < contor_linieM; k++)
	cout << total_liniiM[k] << endl;

}



int main()
{

    citire_fisier_instr();
    cout<<" " <<endl;
    citire_fisier_mne();



}


Last edited on
I'm not sure I can decode your algorithm there. Comments in your code would help. You should always comment you code. Those variable names are either words I do not recognize or not in English.

There's no need to allocate new strings. That's the beauty of the Standard Template Library. As you read in each line, push them back into a vector. std::vector will manage the memory for you. Just keep track of how many you've pushed in, because vector will double in size when it needs more room, then it will likely have empty spots, and you don't need to look at those.

std::vector is a beautiful thing, and it is always my default for storing items in a container.

http://www.cplusplus.com/reference/vector/vector/

You can then extract each string in the vector, look at the first 3 strings, and compare it to the strings in the vector that you created in for the other file. Your files look eerily similar to assembly language.
Topic archived. No new replies allowed.