<< Chapter < Page Chapter >> Page >

Example

#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.

Structures

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 .

Example

#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

Arrays of structures

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:

Output of program

Questions & Answers

prostaglandin and fever
Maha Reply
Discuss the differences between taste and flavor, including how other sensory inputs contribute to our  perception of flavor.
John Reply
taste refers to your understanding of the flavor . while flavor one The other hand is refers to sort of just a blend things.
Faith
While taste primarily relies on our taste buds, flavor involves a complex interplay between taste and aroma
Kamara
which drugs can we use for ulcers
Ummi Reply
omeprazole
Kamara
what
Renee
what is this
Renee
is a drug
Kamara
of anti-ulcer
Kamara
Omeprazole Cimetidine / Tagament For the complicated once ulcer - kit
Patrick
what is the function of lymphatic system
Nency Reply
Not really sure
Eli
to drain extracellular fluid all over the body.
asegid
The lymphatic system plays several crucial roles in the human body, functioning as a key component of the immune system and contributing to the maintenance of fluid balance. Its main functions include: 1. Immune Response: The lymphatic system produces and transports lymphocytes, which are a type of
asegid
to transport fluids fats proteins and lymphocytes to the blood stream as lymph
Adama
what is anatomy
Oyindarmola Reply
Anatomy is the identification and description of the structures of living things
Kamara
what's the difference between anatomy and physiology
Oyerinde Reply
Anatomy is the study of the structure of the body, while physiology is the study of the function of the body. Anatomy looks at the body's organs and systems, while physiology looks at how those organs and systems work together to keep the body functioning.
AI-Robot
what is enzymes all about?
Mohammed Reply
Enzymes are proteins that help speed up chemical reactions in our bodies. Enzymes are essential for digestion, liver function and much more. Too much or too little of a certain enzyme can cause health problems
Kamara
yes
Prince
how does the stomach protect itself from the damaging effects of HCl
Wulku Reply
little girl okay how does the stomach protect itself from the damaging effect of HCL
Wulku
it is because of the enzyme that the stomach produce that help the stomach from the damaging effect of HCL
Kamara
function of digestive system
Ali Reply
function of digestive
Ali
the diagram of the lungs
Adaeze Reply
what is the normal body temperature
Diya Reply
37 degrees selcius
Xolo
37°c
Stephanie
please why 37 degree selcius normal temperature
Mark
36.5
Simon
37°c
Iyogho
the normal temperature is 37°c or 98.6 °Fahrenheit is important for maintaining the homeostasis in the body the body regular this temperature through the process called thermoregulation which involves brain skin muscle and other organ working together to maintain stable internal temperature
Stephanie
37A c
Wulku
what is anaemia
Diya Reply
anaemia is the decrease in RBC count hemoglobin count and PVC count
Eniola
what is the pH of the vagina
Diya Reply
how does Lysin attack pathogens
Diya
acid
Mary
I information on anatomy position and digestive system and there enzyme
Elisha Reply
anatomy of the female external genitalia
Muhammad Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask