String issues

Ok, I'm having a few problems with strings, mostly string functions saying they're not able to compare a string with a char pointer.

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
int main()
{
	int counter = 0;
	int x, y, z;
	string command;
	char * pch;
	string a, b, c, d, e, f;
	cout << "Reservations>>";
	cin.get (command);
	pch = strtok (command," ");
	if(command[0] == "n"){
		while(pch != NULL)
		{
			if(counter == 0)
				a = pch;
			if(counter == 1)
				b = pch;
			if(counter == 2)
				c = pch;
			if(counter == 3)
				d = pch;
			//need to convert char *d to int
			pch = strtok (NULL, " ");
			counter++;
		}
		strcat (a,b);
		if (strcmp (a, "newflight") !=0)
			Flight(c,d);
	}


My goal is to take in a command and store it in a string. Different commands have different amounts of information I need. One command is "new flight <flightnumber> <seats available>". First, I try to understand which command is being asked, using the first character to decide. Once I can assume which command is being attempted, I try to separate the string into smaller strings (words) using strtok. I then compare the words that should be constant (new and flight), to make sure the command is syntactically correct (which I use strcmp for). Then I'll go on to create a new flight, which is a class that takes in a char * and integer (which is why I need to convert a char * to integer).

Thanks for any help, feel free to point me in the direction of any guide on why I can't using string commands on these strings..
Last edited on
because the C++ string type isnt the same as c style char strings which store each character in a string as an array.
you can use the method from http://www.cplusplus.com/reference/string/string/c_str/

1
2
3
4
  std::string str ("Please split this sentence into tokens");

  char* cstr = new char [str.length()+1];
  std::strcpy (cstr, str.c_str());


and then use that to compare with the C string functions.

Last edited on
new flight <flightnumber> <seats available>
example new flight 207 64

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

using namespace std;

int main()
{
	char arr[19] = {0};
	int iTempVlue = 0
	cin.getline(arr,19);                 //for getting the line with spaces 
	sscanf(&arr[11],"%d",&iTempVlue);    //207
	sscanf(&arr[14],"%d",&iTempVlue);    //64
	return 0;
}


and for comparing use if or switch as you want..
Last edited on
Topic archived. No new replies allowed.