Is there a way to represent an integer character?

I am currently creating a find function to locate a specific piece of data in a different lines. Using the std::size_t found = data[i].find(str); However the problem I am facing right now is that the criteria I am using to locate this piece of data changes sometimes depending on the line and the data preceding it. Is there a way to represent an integer value in a string without having to declare 10 strings for each number?

Example
 
std::string str2 = "0A,", str6 = "1A,", str7 = "2A,",str8 = "3A,", str9 = "4A,", str10 = "5A," ;
I don't understand the question.
"0A4B" is possible. you don't need to make 1 string for each hex byte.
you can also convert an integer value to a string (and in hex if you like). Is that what you want to do? Or something else?
Thanks for replying jonnin, what I am trying to do is locate a piece of data called data2 that is always located after another piece of data called data1. However data1 varies in each line and I cannot narrow the search parameter to just "A," since the find function may locate another piece of data preceding data1 matching that same parameter. So I have currently created multiple strings and using multiple if statements where each if statement takes a different string to account for the values 0-9. What I would like to do would be to use 1 string that would account for all the numbers 0-9 lets say std::string str = "#A,"; where # represents any integer 0-9. I am not sure if that is possible but at the moment even declaring multiple strings and using the multiple if statements I am locating the wrong piece of data. I have been at this for over 30 hours total programming time on this one function. Any assistance would be appreciated.
this sounds like a regular expression. do you know regx, and that c++ supports them? I am terrible at them; I always have to go online somewhere to craft the expression and test it, but then again, I use one like once in 2-3 years.

or, you may be able to split the find.

if you are looking for "a ???? lazy" pattern in "the quick brown fox jumped over a lazy dog"
then find(a, string) and find (lazy, string) gets you 2 points in your string. loop in the middle to see if everything else between the 2 is a space or a digit, and if so, you got it. But that kind of parsing means knowing the data and not supporting a lot of patterns. I do this all the time in xml with find tag, find end tag, take a look at whats between them.
Last edited on
I have seen the term regx or regex before, not sure which one exactly. I don't know what they do but I will look in to them if that would help me in this situation. I have just looked over my multiple if statement finders and I realized that since I added a integer in the string I am supposed to state x = found + 3; instead of x = found + 2; So I will also retry my program after doing this fix and see if that does what I need it to do, but if the regx will help simplify my problem I would rather use that than these if statements because I also have to include another 10 strings since the letter in the string "#A," also varies with another letter "#B,". Thank you for the assist.
it stands for regular expression and they let you match patterns in strings instead of matching constants or trying to re-invent a way to do that with complex conditions and iterations.

they use a standardized set of symbols to express the pattern, and that is almost a programming language unto itself, but what you are asking... you can find an example of it I think, its not too bad. The more complex the pattern the more complex the expression, but matching any number is a common one.

consider
https://www.regular-expressions.info/stdregex.html
Last edited on
Tell us what you are trying to match (use examples) and we will write some C++ code to do a regex match (or search & replace) for you.

[edit] Or, better yet, if you can write yourself a nice regex using the link jonnin gave you, that would help a lot.
Last edited on
Thanks for the replies guys and sorry for the brief hiatus, I had some mid semester exams. As you have recommended me to try regex I have managed to create a expression to find the data I am looking for. I have not implemented it in a large scale for the entire file, but so far it works for the very specific cases that are hard to get. I will post again when I have tested it and let you all know the result.

EDIT: Is it possible to search through an array with regex?
Last edited on
yes. an array of what, though (strings?). You would need a loop, of course.
Yes an array of strings. I am currently using the search loop but not sure if I should include a loop within that loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
void match(std::string str[], std::regex reg)
{
	int i = 0;
	std::smatch match;
	while (std::regex_search(str[i], match, reg))
	{
		std::cout << "Number Of Matches " << match.size() << std::endl;
		std::cout << match.str[i](0) << '\n' << std::endl;
		str = match.suffix().str[i]();
		std::cout << '\n';
                i++;
	}
}
Last edited on
I managed to get the loop to work but now I have a new problem, when I reach the last line in the array the function finds a match but I get an error "ConsoleApplication19.exe has stopped working". I limited the loop to match the second to last string and break out the loop and it exited with no problem so I'm confused as to why I am getting that problem on the last string. Any assistance would be appreciated.
Topic archived. No new replies allowed.