Wednesday, June 30, 2010

Excercise 1 chapter 3 and arrays

Here is the code we did in class


#include
using namespace std;

void ConvertInches(int inches);

int main()
{
cout << "Please Enter your height in inches." << endl;
int inch;
cin << inch;
ConvertInches(inch);

char c;
cin << c;
}

void ConvertInches(int inches)
{
const int FOOT=12;
int heightInFeet = inches / FOOT;
int remainingInches = inches % FOOT;

cout << "you are " << heightInFeet << " feet and "
<< remainingInches << " inches tall." << endl;

}

Here is the code for the arrays

#include
#include //add the string library
using namespace std;

int main()
{
const int size =5; //declare a constant

int myArrayb[size]; //arrays require a constant for limit

int myArray[4] = {4,7,23,16}; //initialize the array

char name[]="George Jetson";//character array


//this gets the character array and the length of
//the character array and then choses the fifth character
//from the array
cout << "Your name is "<< name << "there are "
<< strlen(name) << " characters in your name"
<< "The fifth character is " << name[4] <<
endl;

//gets the third element from the first array
cout << "the 3rd element is " << myArray[2] << endl;

//use of getline with a character array
cout << "enter your first and last name" << endl;
char fullName[25];
cin.getline(fullName, 25);

string address; //declare a variable as string
cout << "Enter your address" << endl;
//use string getline
getline(cin, address);

cout << "Your name is " << fullName << endl;
cout << "Your address is " << address << endl;

//just pause the program waiting for a character
//to be entered
char c;
cin << c;


}

No comments:

Post a Comment