Got an input, how do I parse only sections of it?

Thanks in advance. I've successfully gotten a text input using the curses library, and successfully comparing it against some other strings. That's cool and all, but what I really want to do is make it like a command line. Like if I type, "add(5, 5)" into my program, it sees that it is a command, and then acts based upon the two different fives. I will be happy to answer anymore questions about what I have done thus far. Thanks.
There exist tools for general parsing, like boost spirit. Although, if you know the format of your input, you may be able to do it in a much simpler way. You'd mostly use string::find and string:substr or a stringstream, depending on what that format is.
Hmm. That makes sense. But what if the input has something the program doesn't expect, like a filename? Sorry if this sounds like I'm just simply dismissing your answer but I looked around on the string functions and couldn't find anything about variable string length searching. Thank you again for your help.
We did something similar to this in class with use of cin.peek()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	char word[256];	
	int count = 0;
	cout << "Enter anything ending in \"(\": ";
	
	while (cin.peek() != '('){
		cin.get(word[count]);
		count ++;
	}
	cin.ignore(80, '\n');

	word[count] = '\0';

	cout << "You typed: " << word;

	cout << "\n\nPress enter to quit";cin.get();
	return 0;
}


Strings make it much easier:
1
2
3
4
5
6
7
8
9
10
	string word;
	int x, y;

	int count = 0;
	cout << "Enter the command ending with \"(\": ";
	
	getline(cin, word, '(');
	cin.ignore(80, '\n');

	cout << "You typed: " << word;


In the long run I think you would be better making an option list instead of figuring out what the person wanted by the one line. This will make your code easier to break into pieces later.
Last edited on
A proper parser may be more flexible when dealing with invalid input, but with a simple one that can be done too.
What do you mean by "variable string length searching"?
http://cplusplus.com/reference/string/string/find/ should satisfy most of your searching needs.
Another option is to use a regex library. I think there is a (somewhat) standard one, so you could try that, if your language is regular.
Topic archived. No new replies allowed.