I'm starting with Arduino programming, and I'm developing a program that will receive an string and a integer, together, for example: "pisca 10". How can I separate it in a String(pisca) and a integer(10) variable? The integer will be applied in a For() loop and the String will be tested with another. How can I do it?
You will have to do it in two steps:
Read the complete string (which is "picsa 10" in your example) and store it into a variable as a String.
Extract the number part from the stored string.
For 2, you can implement this yourself as an exercise, but you can also use sscanf function which purpose is to parse a string according to a format. Please see the example in the linked page.
Related
In my code the following line gives me data that performs the task its meant for:
const char *key = "\xf1`\xf8\a\\\x9cT\x82z\x18\x5\xb9\xbc\x80\xca\x15";
The problem is that it gets converted at compile time according to rules that I don't fully understand. How does "\x" work in a String?
What I'd like to do is to get the same result but from a string exactly like that fed in at run time. I have tried a lot of things and looked for answers but none that match closely enough for me to be able to apply.
I understand that \x denotes a hex number. But I don't know in which form that gets 'baked out' by the compiler (gcc).
What does that ` translate into?
Does the "\a" do something similar to "\x"?
This is indeed provided by the compiler, but this part is not member of the standard library. That means that you are left with 3 ways:
dynamically write a C++ source file containing the string, and writing it on its standard output. Compile it and (providing popen is available) execute it from your main program and read its input. Pretty ugly isn't it...
use the source of an existing compiler, or directly its internal libraries. Clang is probably a good starting point because it has been designed to be modular. But it could require a good amount of work to find where that damned specific point is coded and how to use that...
just mimic what the compiler does, and write your own parser by hand. It is not that hard, and will learn you why tests are useful...
If it was not clear until here, I strongly urge you to use the third way ;-)
If you want to translate "escape" codes in strings that you get as input at run-time then you need to do it yourself, explicitly.
One way is to read the input into one string. Then copy the characters from that source string into a new destination string, one by one. If you see a backslash then you discard it, fetch the next character, and if it's an x you can use e.g. std::stoi to convert the next few characters into its corresponding integer value, and append that number to the destination string (either adding it with std::to_string, or using output string streams and the normal "output" operator <<).
In the textbook "Starting Out In C++" by Gaddis in chapter 1 the author says that some numbers like zip codes are intended for humans to read, to be printed out on the screen to look at and to not calculate with so they should be stored in string data type not numeric data types. But there is a couple of other reasons why this statement is true. The only other reason why I can think this would be true is if you were to enter a zip code with an ending like 37217-1221 you may have to use string catenation to only use the first five digits chopping of the characters after the -1221. What would be some other reasons for the statement "If a number is not going to be used in an arithmetic operation, store it in a string data type". Any answers would be greatly appreciated.
Zipcodes simply are not numeric data. As you point out, zipcodes can contain extensions, which numeric data does not represent. They can also contain significant leading zeros. Some postal code schemes can also contain letters.
Your questions was a bit...not of a questions? That's the best I can explain it. Anyway, a string is text and an integer or number is numerical and should only be used for calculations or counting. For example:
A zip code is a number but you will never do calculations with it. A zip code is something you reference as a place and has no counting purpose. If you think this could confuse you later on try to give the variable with the zip code an assignment of a String so that you cannot try to do any sort of math with the variable.
I am trying to learn c++, and for an assignment I have ran into a problem. I am trying to get an integer value from a string that a user enters all on one line.
Ex.) The user inputs: "Change value to 15."
What is the best way of getting the 15 from that string? I have looked around for a while, but could only find if a string was only integers.
Thanks in advance!
Why not use a mixture of getline(grabs your whole line) and string stream(tokenizes the input) and put them all in a vector(easier to use than an array), grab the one at .size()-1 and do an atoi on that. Might be overkill, but string stream could do what you want. For a small tut this could help http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
This might not be the best way but to get something done now you can use strtok to tokenize your input string and then examine the tokens for your integer value integer. One of the answers here [link] suggests using strtok to tokenize a string.
If you know the format of you string or know that there will always be a single integer value then you can use string::find_first_of and string::find_last_of [link] and then just get the substring and use string::stoi.
I am working through C++ program design by Cohoon and Davidson. This is what it says about string class attributes (3rd Edition, Page 123):
Characters that comprise the string
The number of characters in the string
My question is: If we know the characters in the string, does not it implies we already know about number of characters in the string? What is the need to explicitly specify the second attribute?
You are right but length is required in many places like counting, or knowing the length/end of malloc memory so it is better to store length as additional property to make your program run fast.
Consider what will happen if the program needs to count the chars all the way just to tell you how many are there in it. Moreover when this feature is accessed frequently.
So it simply saves time storing length too.
So all actual implementations of string classes do store length of the string.
If we know the characters in the string, does not it implies we already know about number of characters in the string?
Well in C we know the number of elements because we can count up to the NULL terminal. But think how expensive it is to get the length of a string? It takes walking the entire string. For such a common operation, why wouldn't we want this to be a constant-time operation?
I have a c++ program, I would like the first argument of the main (argv[1]) to correspond to a table of float. Is it possible to do that??
I was thinking about putting in a string my floats separated with spaces (e.g. "1.12 3.23 4.32 1.1 ...")
Is there a way to automatically convert such a string into a table of floats? If I understand well the atof function converts a string into a double. So it seems it could be possible to split my string using the spaces and then convert each portion using atof.
This option does not seem to be very efficient to me? In addition it returns double and not float :(
So, is there a better way to pass table of float as argument of a c++ program ?
Thank you
A stringstream can do both the splitting at spaces and the parsing into a float.
std::stringstream ss(the_string);
std::vector<float> v(std::istream_iterator<float>(ss),
(std::istream_iterator<float>()));
// the extra parentheses here are ugly but necessary :(
How to obtain the string with the data depends on how large it is and where it is supposed to come from. Just keep in mind that in many systems the arguments passed to program are already split by spaces, putting each part in a different element of argv.
Save it in a text file, and then read it from the file when your program starts. I isn't worth it to pass it as a command-line argument.
The main() parameter list is as it is. You can pass the strings of your numbers as arguments to your program. The main function will have to parse its argument.
When you want to pass a space separated list of numbers in argv[1] you can use the strtok function to get the individual number strings and have to pass it to a conversion function.
When your conversion function returns a double you should check that the result can be represented by a float and cast the value to a float variable. But I would consider to use double as the internal representation.
In addition to Singer's answer:
The commandline should be used mainly by human, not computer. If you really need a table of values, use configuration file. You can always use human readable format for it.