Monday, June 28, 2010

First C++

Here is the first C++ code example:



#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int cube(int); //function prototype

int main()
{
/* this is a simple hello application
the comment can cross
several lines */
cout << "Enter your name" << endl;
char name[25];
cin.getline(name, 25);

cout << "Hello," << name << endl;

cout << "Enter a number" << endl;
int num;
cin >> num;
cout << "the cube of " << num << " is " << cube(num) << endl;
cout << "The square root of " << num << " is " << sqrt((double)num) << endl;
//this is just to pause the console
char c; //declare a character type variable
cin >> c; //wait for console entry
}

int cube (int x)
{
return x * x * x;
}

No comments:

Post a Comment