Compare Strings, Strncmp error!?

Hy!

In "instr.txt" i have:
1
2
mov ax,03
add ax,05


I read lines from file in separate strings, and i want to compare each first 3 characters from each strings with some text (mov,add,sub,mul).
Something like(i ned to figure out how to implement this 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
 if (strncmp (all_lines[0],"mov",2) == 0)
    {
     //put ax into a char
     // put 03 into a variable
     // then ax=3
     }
else 
    if (strncmp (all_lines[0],"add",2) ==0)
          {
            //put ax into a char
           // put 03 into a variable
           // then ax = 'another variable' + 3
          }
else
     if (strncmp (all_lines[0],"sub",2) ==0)
    {
          //put ax into a char
           // put 03 into a variable
         //then ax = 'another variable' - 3
   }
else  if (strncmp (all_lines[0],"mul",2) == 0)
        {
          //put ax into a char
           // put 03 into a variable
          //then ax = 'another variable' *3
            }





Here's the main 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
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

int main()
{

    ifstream myFile;
	string line, *all_lines;
	int line_count = 0;
	myFile.open("instr.txt");
	while (getline(myFile, line))
		line_count++;
	all_lines = new string[line_count];
	int i = 0;
	myFile.close();
	myFile.open("instr.txt");
	while (getline(myFile, line))
	{
		all_lines[i] = line;
		i++;
	}
	for (i = 0; i < line_count; i++)
		cout << all_lines[i] << endl;



		 if (strncmp (all_lines[0],"mov",2) == 0)
    {
      cout<<"found"<<endl;
    }


}




My quest are:
1.In the main code , i get the error: "cannot convert str::strings{aka std::basic_string<char>} .
2.I need some advices about how i can to implement the compare strings code.

Thanks! :)
Last edited on
Since you are using std::strings you don't need to use the C-string functions.

C++ strings can be compared with the comparison operators ==, !=, >, <, etc. or there is the compare() member function that works much like strncmp() in C.
http://www.cplusplus.com/reference/string/string/compare/
Thaks a lot jlb .
Last edited on
Topic archived. No new replies allowed.