String Data and I/O
Dr. Tim McGuire
CS 164
Sam Houston State University


What You'll Learn

Character Literals and Variables 'A' 'x' ' ' '*' '&' '1' char MyGrade; //Defines a character variable
MyGrade = 'A'; //Stores char data in the char variable
char MyGrade = 'A'; //Defines and initializes
Defining and Storing Values in Three Character Variables void main()
{ char first, middle, last;
first = 'T';
middle = 'J';
last = 'M';
// Rest of program would follow . . .
String Literals "C++ Programming" "123" "X" "CS 164"
"That depends on what the meaning of 'is' is"
cout << "Dr. McGuire makes C++ fun!" String Endings String Lengths An Example // Filename: STLITPR.CPP
// Printing string literals is easy
#include <iostream.h>
int main()
{ cout << "This is a string literal.";
return 0;
}
Special Characters: Escape Sequences Some Important Escape Sequences Using Escape Sequences cout << "\"This string is in quotes\""

would output

"This string is in quotes"

To correctly output the DOS pathname of a file use

cout << "C:\\temp\\newfile.txt"

instead of

cout << "C:\temp\newfile.txt"

Characters and Integers char c;
c = 'A' + 7;
Example mixing integers and characters // Filename: INTCHAR.CPP
// Mixing integers and characters
#include <iostream.h>
int main()
{ int i, j, k; // Define 3 integers
char c, d, e; // Define 3 characters

i = 'A'; // Stores 65 in i
j = 'B'; // Stores 66 in j
k = 'C'; // Stores 67 in k

c = 88; // Stores 'X' in c
d = 89; // Stores 'Y' in d
e = 90; // Stores 'Z' in e

cout << i << ", " << j << ", " << k << endl;
cout << c << ", " << d << ", " << e << endl;

return 0;

}
Storing Strings in Arrays char fish[6]; // Define an array Defining Strings char fish[6] = "trout"; /* Define and store a value in an array */ char fish[] = "trout" /* Define and store a value in an array */ Individual Characters in a String fish[0]=‘t’, fish[1]=‘r’, fish[2]=‘o’, fish[3]=‘u’, fish[4]=‘t’, fish[5]=‘\0’ Working with Character Arrays cout << "Here is a string: " << astring << endl; Using strcpy() Getting Keyboard Input Example of Input char input[80]; cout << "Please type the answer: ";

cin >> input;

Validating Input Storing Strings in Character Arrays Comparing cout and cin Using cout and cin together cout << "How much do you make? ";

cin >> salary; // waits for user to type a number

Output Options // cout can output integers:
cout << 45;          // prints a 45

// cout can output floating-point values:
cout << 1.234567     // prints 1.234567

// cout can output string values:
cout << "I am learning C++";

I/O Manipulators precision() and flags cout.setf(ios::fixed); // sets the cout flag for fixed point
cout.setf(ios::showpoint) // always shows decimal point
cout.precision(2); // two decimal places
cout << 4.5;
The "stickiness" of setprecision() cin.ignore() cin.ignore(80,'\n'); // flush the input buffer Width of Output Fields cout << setw(12) << 1.345678 << endl;

cout << setw(8) << "xyz" << endl;

cout << setw(6) << 12 << endl;

 1.345678
  xyz
 12
setw() Example Hex and Octal Output Input Options cin >> result; cin >> a >> b >> c; Getting Lines of Data cin.getline() getline() Examples