Monday, June 27, 2011

C++ Beginning Examples

Here are the beginning examples of C++ we did in class. They are a bit of a mess, but show a lot of basic features.


#include <iostream>
#include <string>
#include <math.h>

/*********************
this is a multiline comment
the namespace includes all the classes
in the standard (std) library
**********************/
using namespace std;

//main is the starting point of any program

//a fucntion must be declared before it can be used
double Cube(double number);
int Remainder(int num1, int num2);

int main()
{
/********************************
this main is a mess containing many different
examples of different things
**********************************/
//the std:: is unnecessary if you have the using statement above
//if not you must use it. the :: is a "membership operator. cout is a member of
//std. cout means console out
std::cout << "Enter your name \n" ;
char name[10]; //declare a character array--the native way to do strings
std::cin >> name; //cin means console in
std::cout << "Hello, " << name << " out there" << std::endl; //endl=end line
cout << endl;
cout << " Enter a number " << endl;
double number; //declare a variable
cin >> number; //get it
int intNumber=(int)number; //cast a double to an int
//ints drop all decimal parts. they don't round
cout << "intNumber: " << intNumber <<endl <<endl;

//size of function for determining the size of a data type
cout << "int is " << sizeof(int) << " bytes" << endl;
cout << "double is " << sizeof(double) << " bytes" << endl;
cout << " Long is " << sizeof(long) << " bytes" <<endl;

//sqrt comes from the math library included above
cout << "The square root of " << number << " is " << sqrt(number) << endl;

//calling our home made function Cube
cout << "the cube of " << number << " is " << Cube(number) ;

//just showing the put method
cout.put('$');

//get integer variables
cout << "Enter the first integer: " ;
int num1, num2;
cin >> num1;
cout << endl;
cout << "Enter the second integer:";
cin >> num2;

//show the use of a modulus
cout << "The quotient of " << num1 << " divided by " << num2 << " is "
<< num1/num2 <<endl;
cout << "The remainder is " << Remainder(num1,num2);

//this is just to pause the console when in VS
char c;
std::cin >> c;
}

double Cube (double number)
{
return number * number * number;
}

int Remainder(int num1, int num2)
{
//the modules returns the remainder of integer division
return num1 % num2;
}


Here is the first exercise in chapter 3 which asks a user to enter his or her height in inches. The program returns the height in feet and inches.


#include <iostream>

using namespace std;

const int CONVERSION = 12;
int GetFeet(int);
int GetInches(int);
void GetInput();

int main()
{
GetInput();

char c;
cin >> c;
}

int GetFeet(int height)
{
return height / CONVERSION;
}

int GetInches(int height)
{
return height % CONVERSION;
}

void GetInput()
{
cout << "Enter your height in Inches " << endl;
int inches;
cin >> inches;
cout << " You are "
<< GetFeet(inches)
<< " feet tall and "
<< GetInches(inches)
<< " inches " << endl;
}

No comments:

Post a Comment