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

I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
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