String Variables and Output/Input Adding Strings and Literals to Your Programming Skills and output/input formatting
A group of characters put together to create text is called a string. Strings are one of the most useful kinds of data in a program Strings help programs communicate with the user and allow computers to process data other than numbers You have already used strings to ask for information from the user
Introduction to Strings and Literals Values that are keyed directly into the source code are often called hard-coded values Values or strings that are hard-coded into the source code are called literals. A hard-coded numeric value is called a numeric literal. A string of text that is hard coded is called a string literal
Introduction to Strings and Literals cont. A single character can also be hard coded A character literal appears in single quotes A string literal appears in double quotes You have already used them in cout statements
Examples of Literals X = 6.3; // 6.3 is a numeric literal cout << Hello ; // Hello is a string literal MyChar = A ; // a is a character literal Literals don t change when the program runs
Null Terminator Tells the compiler the string has ended An invisible character at the end of a string literal The character represent by the ASCII value zero. In C++ it is \0 together representing one character
M A R K G E N T R Y \0 Null Terminator
Character Arrays Used to store strings that change as the program runs. A group of variables of the same data type that appear together in the computer s memory A group of variables of the type char. Each char variable holds an ASCII value fo the character, and the null terminator. You can devote exactly the amount of memory you need to strings and easily manipulate individual characters in strings.
Declaring Character Arrays Characters K A L E Y \0 ASCII Value 75 65 76 69 89 0 Char student_name[21]; is and example of a declared character array This allows an the string variable student-name to contain 20 printable characters and the null terminator. ALWAYS save room for the null terminator when declaring character arrays that have different lengths.
Zip Codes and Telephone Numbers Phone numbers in character arrays can include parentheses and hyphens which are not allowed in numeric data types
Using the String Class To use the string class, you must include a header file in your source code In most cases it is #include apstring.h, #include apstring.cp or #include<string>
strcpy Function Pronounced string copy A sting literal can be stored in a character array or one character array can be copied to another Have to be sure the character array receiving the new data is long enough to house it
strcpy(student-name, Aliver Villarreal ); Used to store a name in the student-name array strcpy(destination-array, source-array); Used to copy the contents of one character array to another ** C++ does not prevent you from storing a string that is too long for the character array, therefore it spills over into other storage spaces and can crash the computer **
Formatting Output \n ; is called the new line character or the end-of-line character. Causes the cursor to return to the next line of the screen when the output is a single character much like the enter key on the keyboard Double quotes must be used if it is used in conjunction with other characters. Ex. cout << String\n ;
Formatting Output cont. \t generates a tab character to move the cursor to the next tab stop \\ prints a backslash (\) \ prints a single quote \ prints a double quote mark ( ) endl; use in place of the character \n For string literals, use << endl; following the quotations
Using setf And unsetf left left-justifies the output right right-justifies the output showpoint displays decimal point and trailing zeros for all floating-point numbers, even if the decimal paces are not needed uppercase displays the e om E-notation as E rather than e showpos displays a leading plus sign before positive values scientific displays floating-point numbers in scientific ( E ) notation fixed displays floating-point numbers in normal notation
Using setf And unsetf example cout.setf(ios::showpoint); Replace showpoint with the other commands. The statement under this statement is the cout with the variable that is to be displayed on the monitor To unset the command use cout.unsetf(ios::***); where *** represents the variable
I/O manipulators - setprecision In order to use the directive #include<iomanip.h> must be included Used in conjunction with the setf(ios::fixed); setprecision manipulator sets the number of decimal places to the number provided in the parentheses ex. cout << setprecision(2) << price << \n the amount in the variable price would show 2 decimal places
I/O manipulators - setw In order to use the directive #include<iomanip.h> must be included setw manipulators are used to change default field widths - ex. cout << setw (10) << I << setw(10) << j << setw(10) << k << endl;
Inputting Characters The get function allows a string containing spaces and almost any other character to be entered into a character array. The string input ends when the user presses Enter. You can specify the maximum number of characters in your statement that you want to allow.
Examples of get Ex. cin.get(student-name, 20); this statement allows for an input of 19 characters as the 20 th is the null terminator Ex. getline(cin, MyString); the MyString is changed to the name of the programmers string object
Flushing the Input Stream cin object is referred to as the input stream The character that was inputted stays in the stream Problem arises because the getline method is looking for any sequence of characters ending ina new line character If it is waiting in the input stream, that s what you get in your string To remedy the problem you must remove the extra characters from the input stream before the getline method is executed. This operation is called flushing the input stream.
Statement for Flushing Always use this statement after the get function, because the get function always leaves a new line character in the stream cin.ignore (80, \n ); This statement tells the program to ignore the next 80 characters in the stream and the \n tells the function to stop ignoring characters when it gets to a new line character. Most programmers use the number 80 to play it safe even though a smaller number might be able to be used.
Use Descriptive Prompts Output prompts that clearly explain the input the program is requesting. If prompting for a telephone number or some other formatted data, you may want to use the prompt to give and example. The more descriptive and clear your prompts, the more likely the user is to enter the information in the form your program is expecting.