<< Chapter < Page | Chapter >> Page > |
#include<iostream.h>
#include<string.h>
int main()
{
char FirstName[25];
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 "<<LastName<<endl;
else if(n ==0)
cout<<FirstName<<" is equal to "<<LastName<<endl;
else
cout<<FirstName<<" is greater than "<<LastName<<endl;
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 first.
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.
A structure , or struct , is an advanced, user-defined 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 elements , field, or members.
You define a structure using the syntax:
struct struct_name{
data_type field_name;
data_type field_name;
……..
} variable_name;
For example, the statement
struct emloyee{
char idnum[5];
char name[40];
long salary;
};
declares the form of a structure named employee and reserves storage for the individual data items listed in the structure. The employee structure consists of three data items or fields.
And the statement
struct emloyee{
char idnum[5];
char name[40];
long salary;
} Emp;
declares that Emp is a structure variable which has the form of the structure employee.
To access the field inside a structure variable, you append a period to the variable name, followed by the field name using the syntax:
variable.field;
When you use a period to access a structure fields, the period is referred to as the member selection operator .
#include<iostream.h>
struct Date // this is a global declaration
{
int month;
int day;
int year;
};
int main()
{
Date birth;
birth.month = 12;
birth.day = 28;
birth.year = 1986;
cout<<"\nMy birth date is "
<<birth.month<<'/'
<<birth.day<<'/'
<<birth.year % 100<<endl;
return 0;
}
The ouput of the above program is:
My birth date is 12/28/86
The real power of structures is realized when the same structure is used for lists of data. Declaring an array of structures is the same as declaring an array of any other variable type.
Example
The following program uses array of employee records. Each of employee record is a structure named PayRecord. The program displays the first five employee records.
#include<iostream.h>
#include<iomanip.h>
const int MAXNAME = 20;
// maximum characters in a name
struct PayRecord // this is a global declaration
{
long id;
char name[MAXNAME];
float rate;
};
int main()
{
const int NUMRECS = 5;
// maximum number of records
int i;
PayRecord employee[NUMRECS] = {
{ 32479, "Abrams, B.", 6.72 },
{ 33623, "Bohm, P.", 7.54},
{ 34145, "Donaldson, S.", 5.56},
{ 35987, "Ernst, T.", 5.43 },
{ 36203, "Gwodz, K.", 8.72 }
};
cout<<endl; // start on a new line
cout<<setiosflags(ios::left);
// left justify the output
for ( i = 0; i<NUMRECS; i++)
cout<<setw(7)<<employee[i].id
<<setw(15)<<employee[i].name
<<setw(6)<<employee[i].rate<<endl;
return 0;
}
The output of the program is:
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?