The cout, cin, function call and array C++ program example

 

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)

Other info: none

To do: Reading from standard input and store the input in an array using cin, cout and function call in C++ programming

To show: How to read input using cin and store them in an array, using cout and doing the function call in C++ programming

 

// cout and cin, function call and array type

#include <iostream>

using namespace std;

 

float simple_calc(float);

 

void main(void)

{

float x = 3, y[5], sum=0;

int i;

// cout with function call

cout<<"Square of 3 is: "<<simple_calc(x)<<'\n';

cout<<"Study the source code and the output"<<endl;

 

for (i=1; i<5; i++)

{

// an array

cout<<"Enter arrays' data #"<<i<<" : ";

cin>>y[i];

 

sum = sum + y[i];

}

cout<<"Sum of the arrays' data is: "<<sum<<endl;

}

 

float simple_calc(float x)

{

float p;

 

p = (x * x);

 

return p;

}

 

Output example:

 

Square of 3 is: 9

Study the source code and the output

Enter arrays' data #1 : 12

Enter arrays' data #2 : 23

Enter arrays' data #3 : 34

Enter arrays' data #4 : 45

Sum of the arrays' data is: 114

Press any key to continue . . .

 

 

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