A very simple C++ array program example demonstrating character and string

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

To do: Using the array type in C++ programming with character and string

To show: How to manipulate character and string using array type in C++ programming

 

#include <iostream>

using namespace std;

 

int main(void)

{

// normal variable and array with their respective data type remember that the last array's index must be reserved for null character

char name[11], name1[11], sex;

 

// write to the standard output

cout<<"\nEnter your first name (max 10 characters): ";

// read from the standard input

cin>>name;

cout<<"Enter your last name (max 10 characters): ";

cin>>name1;

cout<<"\nEnter your sex (M or F): ";

cin>>sex;

 

// test whether male or female

if (sex == 'M')

// array name without brackets is the pointer to the first array's element

cout<<"\nHow are you, Mr. "<<name<<" "<<name1<<endl;

else

cout<<"\nHow are you, Ms/Mrs. "<<name<<" "<<name1<<endl;

return 0;

}

 

Output example:

 

Enter your first name (max 10 characters): Mike

Enter your last name (max 10 characters): Star

Enter your sex (M or F): M

How are you, Mr. Mike Star

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index