Download PROGRAMMING FUNDAMENTALS IN C++

Transcript
CHAPTER 2. LECTURE NOTES
126
char LastName[25];
char FullName[50];
strcpy(FirstName, "Mike");
strcpy(LastName, "Thomson");
strcpy(FullName, FirstName);
strcat(FullName, " ");
strcat(FullName, LastName);
cout FullName endl;
int n;
n = strcmp(FirstName, LastName);
if(n<0)
cout FirstName " is less than " LastNameendl;
else if(n ==0)
cout FirstName " is equal to " LastNameendl;
else
cout FirstName " is greater than " LastNameendl;
return 0;
}
The
output of the program:
Mike Thomson
Mike is less than Thomson
How to input a string
Inputting a string from a keyboard requires the string I/O library function cin.geline(). The cin.getline()
function has the syntax:
cin.getline(str, terminatingLength, terminatingChar)
where str is a string or character pointer variable, terminatingLength is an integer constant or variable
indicating the maximum number of input characters that can be input, and terminatingChar is an optional
character constant or variable specifying the terminating character. If this optional third argument is omitted,
the default terminating character is the newline (`\n') character.
The function call stops reading characters when the terminatingChar key is pressed or until terminatingLength characters have been read, whichever comes rst.
Example
#include<iostream.h>
int main()
{
char Text[40];
cin.getline(Text, 40, `\n');
cout Text endl;
return 0;
}
The cin.getline() function continously accepts and stores characters typed at the keyboard into the
character array named Text until either 39 characters are entered (the 40th character is then used to store
the end-of-string marker,
\0),
or the ENTER key is detected.
2.5.16 Structures
A
structure,
or
struct,
is an advanced, user-dened data type that uses a single variable name to store
multiple pieces of related information.
The individual pieces of information stored in a structure are referred to as
You dene a structure using the syntax:
struct struct_name{
data_type field_name;
elements, eld, or members.