Sei sulla pagina 1di 97

CHAPTER THREE

An Overview of C++

PART ONE
Simple Programs

3.1.1 Your First C++ Program


Before getting into any theory, lets look at a
simple C++ program.

Example(1)
/*Program #1 A first C++ program.
Enter this program/, then compile and run it.
*/
# include <iostream.h>
# include <conio.h>
// main ( ) is where program execution begins.
int main ) (

cout << "This is my First C++ Program .";


getch( ) ;

return 0 ;
}

When run, this program displays the following text on


the screen:
This is my First C++ Program .
Before we continue, it is necessary to define two
terms.
source code:
which is the version of your program that humans can read.
The preceding listing is an example of source code.

Object code or executable code:


which is the executable version of your program (source code)
and it is created by the compiler when it compiles your

Source

C++
Compiler

Code

Object

Code

Exercise:
Write a C++ program to print the following
statement on the screen
"Hi, my name is Saied , What is your name?"

3.1.2 A second simple program


Example( 2)
The following program creates a variable called value,
gives it the value 1023, and then display the message
"This program prints the value: 1023" on the screen.

// program #2 using a variable

# include < iostream.h>


# include < conio.h>
int main ( )

{
int value ; // this declares a variable
value = 1023 ; // this assigns 1023 to value
cout << " This program prints the value=";
cout << value ; // This displays 1023

getch ( );
return 0;
}

The output of this program is:

This program prints the value = 1023

3.1.3 A more practical example

Example( 3)
Write a program to convert gallons to liters

( gallon = 4 liters)

The program:
// This program converts gallons to liters.
# include < iostream.h>
# include <conio.h>
int main ( )
{
int gallons , liters ;
cout << "Enter number of gallons :";
cin >> gallons ; // this inputs from the user
liters = gallons * 4 ; // convert to liters
cout << "Liters = " << liters ;
getch ( );
return 0 ;
}

The output is:

Enter number of gallons:

Liters = 40

10

3.1.4 A Quick Review


Let us review the general form of a C++ program.
# include < iostream.h>
# include <conio.h>
int main ( )
{
int ,

; // integer data type

float ,

; // floating point data type

cout<<"
getch( );
return 0;
}

"<< -------- ;

Exercises:
1- Explain the above program. And use float value instead of
integer value
2- Write C++ program converting US dollars($) to Egyptian
pounds
)1$=5.52 Egyptian pounds (
3- Write C++ program converting Egyptian pounds to US
dollars
(1Egyptian pound= 0.18115942 $ (
4- Write C++ program converting Kilo Bytes to Bytes

) 1 K.B. = 1024 B(
5- Write C++ program converting Mega Bytes to Kilo Bytes
1 M.B.= 1024 K.B(

6- Write C++ program converting Gega Bytes to Mega Bytes


(1G.B. = 1024 M.B.)
7- Write C++ program converting Tera Bytes to Gega Bytes
) 1T.B. = 1024 G.B.(

8- Correct the following statements if it has errors according to


C++ rules.
*/ comment /*
Correction is : /* comment */ OR //comment
/*?????????? //
Correction is : /*?????????? */

But this correction is wrong: ?????????? // why!!!


*/AAAAAA/*
Correction is : /*AAAAAA*/ OR

//AAAAAA

include <iostream.k> ;

Correction is: # include <iostream.h>


*include <iostream.h>
Correction is: # include <iostream.h>
# include <iosteram.h>;
Correction is: # include <iostream.h>
# inklude <isotream.h>;
Correction is: # include <iostream.h>

# include>conio.k<
Correction is: # include <conio.h >

int main

Correction is: int main( ) ;


ind main[ ] ;
Correction is: int main( ) ;
int main { };
Correction is: int main( ) ;
int main ( )
Correction is: int main( ) ;

int main ( )
Correction is: int main( ) ;

return o;

Correction is: return 0 ;


return 0,

Correction is: return 0 ;


return 0;

The statement is correct

gtch ( )

Correction is : getch( ) ;
getkh ( ) ;

Correction is : getch( ) ;
getch ( ),

Correction is : getch ( );
getch ( );

The statement is correct

cout<"Hi",

Correction is : cout<<"Hi" ;
cout << "Hi";

The statement is correct


cout << "Hi;

Correction is : cout << "Hi";


cout <<"Hi";

The statement is correct

cin <<gallons;

Correction is : cin>>gallons;
cim>>gallons;

Correction is : cin>>gallons;
cin>>gallons;

The statement is correct

9- Correct the following C++ program and find its output as


the computer do.
# include<iostream.h>
# include<conio.h>
int main ) (
int value ;

value = 100 ;
cout << " value = " value ;
getch ( ) ;
return 0 ;

The correction of (9)


# include<iostream.h>
# include<conio.h>
int main ) (
{
int value ;
value = 100 ;
cout << " value = " <<value ;
getch ( ) ;
return 0 ;
}

the output is value = 100

PART TWO
FUNCTIONS

A C++ program is constructed from building blocks


called functions.
A function is a subroutine that contains one or more C++
statements and performs only one task.
Each function has a name that is used to call it.
In general, you can give a function whatever name you
please.
Remember that there are words reserved for C++ built in
functions such as main( ) .
You do not use the reserved words as a function name
but you can change the reserved word (deleting some
letters or adding some letters) before use it.

3.2.1 A Program with Two Functions


The following program contains two functions :
main( ) and myfunc( ).
Before running this program, examine it closely
and try to figure out exactly what it displays on
the screen.

Example(4)

/*This program contains two functions :main( ) and myfunc( )


*/
#include <iostream.h>

#include <conio.h>
void myfunc ( );
int main ( )
{
cout <<"In main( ) ";

myfunc( ) ; // call myfunc ( )


cout<< " Back in main ";
getch ( );

return 0 ;

}
void myfunc ( )
{
cout << " Inside myfunc( ) ";
}

The program works like this.

First, main( ) begins, and it executes the first cout statement.


Next , main ( ) calls myfunc( ).
Next , myfunc( ) executes its cout statement, and then returns to
main ( ) at the line of code immediately following the call.
Finally, main( ) executes its second cout statement and then
terminates.

The output on the screen is this:


In main ( ) Inside myfunc( ) Back in main ( )
As you can see, myfunc( ) does not contain a return statement
and does not return a value.
In C++, functions that dont return values are declared as void.

3.3.2 Function Arguments


It is possible to pass one or more values to a function.
A value passed to a function is called an argument.
In the programs that you have studied so far, none of the
functions( main( ) and myfunc( ) )take any arguments.
However, functions in C++ can have one or more arguments.
The upper limit of an arguments number is determined by the
compiler you are using, but Standard C++ specifies that a function
must be able to take at most 256 arguments.
When you create a function that takes one or more arguments,
the variables that will receive those arguments must also
declared.
These variables are called parameters of the function.

Example(5)

// A simple program for a function of two arguments.


#include<iostream.h>
#include<conio.h>

void mul (int x , int y):


int main ( )
{
mul (10,20);
mul (5 , 6);

mul (8 , 9 );
getch ( );
return 0;

void mul (int x , int y)


{
cout << x * y<<" ";

This program will print 200, 30 and 72 on the screen. When mul( ) is
called, the C++ compiler copies the value of each argument into the
matching parameter. That is, in the first call to mul( ), 10 is copied into x
and 20 is copied into y. In the second call, 5 is copied into x and 6 into y.
in the third call, 8 is copied into x and 9into y.

REMEMBER:
The term argument refers to the value that is used to call a function. The
variable that receives the value of an argument is called a parameter.

3.2.3 Functions Returning Values


Many of the C++ library functions that you use will return
values. Functions you write may also return values to the
calling routine.
In C++, a function uses a return statement to return a value.
The general form of return is: Return value;
Where value is the value being returned . To illustrate the
process of functions returning values ,the last program can be
rewritten as shown next . In this version, mul( ) returns the
product of its arguments.

Example(6)
// Returning a value
#include<iostream.h>
#include<conio.h>
int mul (int x , int y);
int main ( )
{
int answer ;
answer = mul( 10 , 11) ; // assign return value

cout<< "The answer is " << answer;


getch ( );
return0;

// This function returns a value.


int mul ( int x , int y)
{

return x * y ; // return product of x and y


}
The output of the program is:
The answer is 110

Example(7)
Write C++ program which containing 4-functions for addition ,
subtraction, multiplication and division of two numbers x , y.

The program
#include<iostream.h>
#include<conio.h>
int add (int x , int y) ;
int sub (int x , int y) ;
int mul (int x , int y ) ;

int div (int x , int y) ;


int main ( )
{

int answer;

answer = add (10 , 2);


cout <<" answer+ = " << answer ;
answer = sub (10 , 2);

cout << " answer- =" << answer ;


answer = mul (10 , 2);
cout << " answer* = " << answer;
answer = div (10 , 2);
cout << " answer/ = " << answer ;
getch ( );
return 0 ;
}
int add ( int x , int y )
{
return x+y ;

int sub ( int x , int y)


{
return x-y ;
}
int mul ( int x , int y)
{
return x*y ;
}
int div( int x , int y)
{
return x/y ;
}

The output is:

answer+ = 12 answer- = 7 answer* = 20 answer/ = 5

3.2.4 Some Output Options


To put a new line character into a string, use this code: \n (a backslash followed
by a lowercase n).

Example(8)
To see an example, try the following program.
/*This program demonstrates the \n code, which generates a new
line.*/

#include<iostream.h>
#include<conio.h>
int main ) (

{
cout << "one\n ";
cout << "two\n ";
cout << "three ";
cout << "four ";

getch ( );

return 0 ;

}
This program produces the following output:

one
two
threefour

Example(9)
Write C++ program which prints the statements Hi ,My name is Saied,
What is your name?. Each statement in a separate line.

The program
#include<iostream.h>
#include<conio.h>
int main ( )

{
cout << "Hi\n" ;
cout << "My name is Saied\n " ;
cout << "What is your name " ;
getch ( ) ;

return 0 ;
}

Example(10)
Write C++ program which containing 4-functions for addition,
subtraction, multiplication and division of two numbers x , y.

The program
# include<iostream.h>
# include<conio.h>
int add (int x , int y );
int sub (int x , int y ) ;
int mul (int x , int y ) ;

int div (int x , int y) ;


) ( int main
{

int answer ;

answer = add (10 , 2) ;


cout <<" answer+ = " << answer;
answer = sub (10 , 2 );

cout << "\n answer- =" << answer;


answer = mul )2 , 10( ;
cout << "\n answer* = " << answer ;
answer = div (10 , 2) ;
cout << "\n answer/ = " << answer ;

getch ( );
return 0;
}

int add ( int x , int y )


{
return x+y ;

}
int sub ( int x , int y)
{
return x-y;
int mul ( int x , int y)

{
return x*y ;
}

int div( int x , int y)

{
return x/y;
}

Guess its output?

Exercise:
1- Write a program which displays the following figure on the screen

*
*

2- Discuss the following program and find its final output


a) #include<iostream.h>
#include<conio.h>
int main ( )
{
cout << "one\n \n";
cout << "two\n \n";
cout <<"three\n \n ";
cout << "four\n \n";
getch ( );
return 0 ;

b) #include<iostream.h>

#include<conio.h>
int main ( )
{

cout <<"\n\n one \n\n ";


cout << "\n\n two \n\n ";
cout << "three\n \n ";
cout << "four\n \n ";
getch ( );

return 0;
}

PART THREE
if Statement & for Loop

Conditions, Logical Expressions, and selection


control Structures
1. To be able to construct true and false statements

to evaluate a given condition.


2. To be able to construct if statements to perform a
specified task.
3. To be able to construct if-else statements to
perform a specified task.
4. To be able to nested if statements to perform a
specified task.
5. To be able to design and implemente a test plan.
6. To be able to debug a program with a selection
control structure.

Conditions, Logical Expressions, and


selection control Structures

Condition

False

True
Statements
Executed when
condition is true

3.3.1 The if Statement


The C++ if statement operates in much the same way that an
if statement operates in any other language.
Its simplest form is :
if(condition) statement ;
Where condition is an expression that is evaluated to be either true
or false.
In C++, true is non-zero and false is zero. If the condition is true,
then the statement will execute.
If it is false, then the statement will not execute.
if(10 < 11 ( cout 10 is less than 11 ;
if(10 < 11 ) x=x+5 ;

The comparison operators:


>

Greater than

>=

Greater than or equal

<

Less than

<=

Less than or equal

==

Equality

Example(11)
The following program shows an example of the if statement. It
prompts the user
for two numbers, and reports if the first value is less than the
second.

// This program illustrates the if statement.


#include<iostream.h>
#include<conio.h>
int main ( )
{
int a , b ;
cout << Enter first number : ;
cin >> a ;
cout << Enter second number : ;
cin >> b ;
if( a < b ) cout << First number is less than second number ;

getch ( ) ;
return 0 ;
}

The output of this program is :


Enter first number : 4
Enter second number : 9
First number is less than second number

3.3.2 The for Loop


The for loop is used to repeat a statement a specified number of
times .
Its simplest form is
for ( initialization ;condition ;increment) statement ;
Here, initialization sets a loop control variable to an initial value.
Condition is an expression that is tested each time the loop repeats.
As long as condition is true (non-zero), the loop keeps running.
Increment is an expression that determines how the loop control
variable is incremented each time the loop repeats.

Example(12)
The following program prints the numbers 1 through 100 on
the screen.
//This program illustrates the for loop statement.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int count ;
for( count=1;count<=100;count=count+1)cout<<count<< ;
getch ( ) ;
return 0 ;
}

The output is :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100

Exercise:
Write a program that prints the numbers 122 through 1000 on the
screen.

Remark:
count=count + 1 equivalent to count ++

count=count - 1 equivalent to count - -

3.3.3 Blocks of Codes


Because C++ is a structured language, it supports the creation of
blocks of code.
A block is a logically connected group of program
statements that is treated as a unit.
In C++, a code block is created
by placing a sequence of statements between opening and closing
curly braces.
In this example,
if(x<10) {

cout<< too low , try again ;


cin>> x ;
}

The two statements after the if and between the curly braces are
both executed only if x is less than 10.
These two statements, together with the braces, represent a block of
code.

Example(13)
The following program uses a block of code. Enter and run the
program so that
you can see the effect of the block.
//This program demonstrates a block of code.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int a , b ;
cout << Enter first number : ;
cin >> a ;
cout << Enter second number : ;
cin >> b ;
if( a < b ) {
cout << First number is less than second number.\n
;
cout<< Their difference is : <<b-a ;

getch ( ) ;
return 0 ;
}

The output is:


Enter first number : 4
Enter second number : 9
First number is less than second number.
Their difference is : 5

3.3.4 Semicolons and Positioning


In C++, the semicolon is a statement terminator.
That is, each individual statement must be ended with a semicolon.
C++ does not recognize the end of the line as a terminator.
For example,

x=y;
y=y+1;
mul( x , y ) ;
is the same as x = y ; y = y +1 ; mul( x , y ) ;
to a C++ compiler.

Exercise:
Use the above section in all pervious programs.

PART FOUR
Arrays

3.4 ARRAYS
This section discusses the array.
An array is a collection of variables of the same type that are referred
to by a common name.
In C++, arrays may have from one to several dimensions, although the
one-dimensional array
is the most common.

3.4.1 One-Dimensional Arrays:


A one-dimensional array is a list of related variables.
The general form of a one-dimensional array declaration is:
type name [size] ;
Here, type declares the base type of the array. The base type determines
the data type of
each element that comprises the array. name defines the array name. size
defines how
many elements the array will hold.
For examples,
int sample [10] ;
declares an integer one-dimensional array named sample that is ten
elements long.
Sample[0]

Sample[1]

Sample[2]

Sample[3]

Sample[4]

Sample[5]

Sample[6]

Sample[7]

Sample[8]

Sample[9]

Where sample[0], sample[1], sample[2], sample[3], sample[4],


sample[5], sample[6], sample[7], sample[8] and sample[9] are
called the elements of the sample array.
float list [7] ;
declares a floating one-dimensional array named list that is seven
elements long.

List[0]

List[1]

List[2]

List[3]

List[4]

List[5]

List[6]

Example(14)
The following program loads the array sample with the numbers 0
through 9:

#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=t;//load the array
for (t=0;t<10;t++)cout<<sample [t]<<" ";//display the array
getch( ) ;
return 0 ;
}

In C++, all arrays consist of contiguous memory locations.


That is, all array elements reside next to each other in memory.
The lowest address corresponds to the first element, and the
highest address to the last element.
0
Sample[0]

1
SAMPLE{!}

2
Sample[2]

3
Sample[3]

4
Sample[4]

5
Sample[5]

6
Sample[6

7
SAMLE{7]

8
Sample[8

9
Sample[9]

Example(14.1)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=9-t;//load the array
for(t=0;t<10;t++)cout<<"sample["<<t<<"]="<<sample[t]<<"\n
";//display the array
getch( ) ;
return 0 ;
}

Example(14.2)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int sample[10];//this reserves 10 integer elements
int t;
for (t=0;t<10;t++)sample[t]=9-t;//load the array
for(t=0;t<10;t++)cout<<"sample["<<t<<"]="<<sample[t]<<"\n
";//display the array
getch( ) ;
return 0 ;
}

Example(15)
The following program creates an array of ten elements, assigns
each element a random value, and then calculates and displays its
maximum and minimum values.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>//Header for the function rand( )
int main ( )
{
int i, minvalue , maxvalue;
int list[10];

for (i=0;i<10;i++)list[i]=rand( );
//find minimum value
minvalue =32767;
for (i=0;i<10;i++)if(minvalue> list[i]) minvalue= list[i] ;
cout<<"minimum value="<<minvalue<<"\n";
//find maximum value
maxvalue =0;
for (i=0;i<10;i++)if(maxvalue< list[i]) maxvalue=list[i];
cout<<"maximum value="<<maxvalue<<"\n";
getch( ) ;
return 0 ;}

Example(16)
Write a program to find the maximum and minimum values
for the array
called saied with 12 elements such that:

22

55

27

15

99

-2

51

#include <iostream.h>
#include <conio.h>
int main ( )
{
int i, minvalue ,maxvalue;
int saied[12];
saied[0]=22;saied[1]=4;saied[2]=9;saied[3]=55;saied[4]=1;
saied[5]=6;saied[6]=27;saied[7]=15;saied[8]=99;saied[9]=3;
saied[10]=-2;saied[11]=51;
for (i=0;i<12;i++)cout<< saied[i]<<" "; //display the array
//find minimum value
minvalue=100;
for (i=0;i<12;i++)if(minvalue> saied[i]) minvalue= saied[i];
cout<<"minimum value="<<minvalue<<"\n";

//find maximum value


maxvalue=0;
for (i=0;i<12;i++)if(maxvalue< saied[i]) maxvalue= saied[i];
cout<<"maximum value="<<maxvalue<<"\n";
getch( ) ;
return 0 ;
}

3.4.2Array Initialization
C++ allows the initialization of arrays. The general form of array
initialization is
Similar to that of other variables, as shown here:
type array name [size] = { value list} ;

Where the value list is a comma-separated list of constants that are typecompatible
with the base type of the array.
The first constant will be placed in the first position of the array, the
second constant in the second position , and so on.
In the following example, a 10-element integer array is initialized with
the numbers
1through 10 :

int i[10] ={1,2,3,4,5,6,7,8,9,10} ;


This means that i[0] will have the value 1 , and i[9] will have the value
10.

Example(16.1)
Use the above notation in example(16)
#include <iostream.h>
#include <conio.h>
int main ( )
{
int i, minvalue ,maxvalue;
int saied[12]={22,4,9,55,1,6,27,15,99,3,-2,51};
for (i=0;i<10;i++)cout<< saied[i]<<" "; //display the array
//find minimum value
minvalue=100;
for (i=0;i<12;i++)if(minvalue> saied[i]) minvalue= saied[i];
cout<<"minimum value="<<minvalue<<"\n";

//find maximum value


maxvalue=0;
for (i=0;i<12;i++)if(maxvalue< saied[i]) maxvalue= saied[i];
cout<<"maximum value="<<maxvalue<<"\n";
getch( ) ;
return 0 ;
}

Example(17)
The following program sorts an array of integers (ascending order) that
contains random values.
// ascending order
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main( )

{
int nums[10];
int a ,b ,t ,size;
size=10;//number of elements to sort
for (t=0;t< size; t++) nums[t]=rand( );//loading the array
//displaying the array
cout<<" original array is : ";
for (t=0;t< size; t++) cout<<nums[t]<<" ";
cout<<"\n";

//sorting the array


for (a=1;a< size ;a++)
for (b=size-1;b>=a ;b--){
if(nums[b-1]> nums [b]){
//exchange elements
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}

//displaying sorted array


cout<<"Sorted array is: ";
for (t=0;t< size; t++) cout<< nums[t]<<" ";
getch( );
return 0;
}

EXERCISE:
Write a program for sorting an array of random integers (descending
order) with size 10
// descending order
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main( )
{
int nums[10];
int a ,b ,t ,size;
size=10;//number of elements to sort
for (t=0;t< size; t++) nums[t]=rand( );//loading the array
//displaying the array
cout<<" original array is : ";
for (t=0;t< size; t++) cout<<nums[t]<<" ";
cout<<"\n";

//sorting the array


for (a=1;a< size ;a++)
for (b=size-1;b>=a ;b--){
if(nums[b-1]< nums [b]){
//exchange elements
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
//displaying sorted array
cout<<"Sorted array is: ";
for (t=0;t< size; t++) cout<< nums[t]<<" ";
getch( );
return 0;
}

PART FIVE
String

3.5 Strings
In C++, a string is defined as a character array that is terminated by a
null.
A null is specified using \0 , and is zero.
Because of the null terminator, it is necessary to declare a character array
to be one character longer than the largest string that it would hold.
Forexample, if you want to declare an array str that could hold a 10
character string, you would write:
char str[11] ;
Specifying the size as 11 makes room for the null at the end of the string.
A string constant is a list of characters enclosed in double quotes.
Here are some examples:
HELLO
I like C++
#$%@@#$
like this:

is called a null string. It contains only the null terminator, and no


other characters.
Null strings are useful because they represent the empty string.

It is not necessary to manually add the null onto the end of string
constants ;
\the C++ compiler does this for you automatically.
Therefore, the string HELLO will appear in memory

\0

3.5.1Reading a string from the keyboard


The easiest way to read a string entered from the keyboard is to
make the array that will receive the string the target of cin statement.

Example(18)
The following program reads a string entered by the user:
//Using cin to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
int main( )
{
char str[80];
cout<<" Enter a string: ";
cin>>str ; //read string from keyboard
cout<< "Here is your string : ";
cout<<str ;
getch( ) ;
return 0 ; }

Although this program is technically correct, there still a problem.


To see what it is, run the program and try entering the string This is a
test .
As you will see, when the program redisplays your string, it will show
only the
word This, not the entire sentence.
The reason for this is that the C++ I/O system stops reading a string
when the first
white space character is encountered.
White space characters include spaces, tabs, and new lines.
To solve the white space problem, you will need to use another of C++ s
library
functions, gets( ).
The general form of a gets( ) call is:
gets( array name ) ;
The header used by gets( ) is <stdio.h>

Example(19)
This version of the preceding program uses gets( ) to allow the entry of
the strings containing spaces.
//Using gets( ) to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
char str[80];
cout<<" Enter a string: ";
gets (str) ; //read string from keyboard
cout<< "Here is your string : ";
cout<<str ;
getch( ) ;
return 0 ;
}

Example(20)
Write a program to read a string from the keyboard using cin.

The Program
//Using cin to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
int main( )
{
char name[80];
cout<<What is your name? ;
cin>>name;
cout<< Here is your name :;
cout<<name;
getch( ); return 0 ; }

Example(20.1)
Write a program to read a string from the keyboard using gets.

The Program
//Using gets to read a string from the keyboard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
char name[80];
cout<<" What is your name? ";
gets(name) ;
cout<< "Here is your name : ";
cout<<name ;
getch( ) ;
return 0 ;
}

Potrebbero piacerti anche