When you're working with float numbers, the amount of numbers following the decimal can be endless. If you wish, there are functions to format the number to your specifications. You can use the fixed to display the output in fixed-point notation. So first off, you would do this: cout << fixed;. Following this line, you must use the function precision(); Inside the parenthesis of this function you will enter the number of decimal places to display in each number. The sytax will look like this: cout.precision(#_of_decimal_places); These must appear before the first cout statement that display's your number. These settings will be appied then on until changed. Here is a code sample using what we just learned:
This would output: 12.35 Because of the 2 inside the parenthesis of precision. And it is rounding up. If you put a zero inside the parenthesis of precision, the program would display: 12 .
Manipulating strings and characters
Their seems to be endless functions that allow you to alter or maiplulate characters and string data. We can change the case, compare portions, replace and insert portions. We can also assign part of a string to another string. We will cover the process of each.
toupper and tolower
You can also change the case of characters in your programs. To accomplish this, you will use the toupper(); and tolower(); functions. These might come in useful if you are getting data from the user and you expect an anwer, but do not expect the case in which they will provide it. So no matter what it is, you can change it to uppercase or to lower case, then you know what you're working with.
The syntax is the same for both:
<variable> = tolower(<char_to_change>);
<variable> = toupper(<char_to_change>);
See this code sample below to check them out in actual code:
To change the case of a string, it will be a bit different because you will need to use the
transform function. In order to use this function however, you will need to include the
algorithm directive at the top of your program (#include algorithm). Heres how you
will use it:
Now before I go along and show a code sample and move on, i'll explain exactly what you're seeing. Ok, to transform the entire string you use string.begin() as the first argument. This refers to the first character of the string.Then use string.end() as the second argument. This will actually refer to the last location at the end of the string. The third argument will store the results of the variable beginning with the first character (if its set to string.begin();). The last part is the function you will use, which will either be toupper or tolower; and will finally change the contents of the variable according to the function you provide. But dont include the parenthesis of the function within the function of transform, this will cause an error. Look at the code sample below for an example:
You can also use the compare(); function to compare portions of two strings. This can be
quite useful, so here is the syntax of the compare function:
Look how you can use it:
In the results of the code above, the compare function will assign 0 to a, because the two characters in the userId variable beginning with the 3rd position(2) follow to be equal to "10".
The function can return a -1, 0 or 1. This is based on the ASCII values to what you compare. Lets assume we have: str1 = "bike" and str2 = "bent" - Say you compared the first character following the second character of the string. The first characters are the same, but the second is different. str1's second character has an ASCII value of: 105, str2's second character has an ASCII value of 101. SO, since str1's char (i) comes after (is greater than) str2's character (e) in ASCII, the compare function will return 1. If it were reversed and str1 was "bent" and str2 was "bike" - the function would return -1 because (e) comes before (less than) the (i).
Replace and Insert
Not only can you compare, but you can then replace and insert. Yes, there's always something else and even at that, theres billions more thats not covered in this tutorial. You use the replace function and can replace portions of strings, just as you compared them earlier. And insert can insert data into existing strings. The syntax looks like this:string.replace(replaceStart, numberOfCharsToReplace, replacementString) - (The syntax is the same for insert.)
Heres an example:
What happens from the code above is the .com is replaced with .net - Look at the replace statement. It starts with position 10 (starting with zero) and then replaced the next 3 positions with what is provided at the end of the function.
No Comments for this page.