Land Area

The problem given is that a client needs to convert between US and European land measurements including input in feet, yards, miles, or furlong and output in feet, English acres, Scottish acres, Irish acres, hectare, Morgen, and hide(std). All input will be entered in length x width and the output will be in all output forms.

Here is the program I wrote to solve the problem:


// *********************************************************************
// Description: Land Area
// *********************************************************************

#include <iostream>            //Preprocessor directive to be able to use cout.
#include <iomanip>             //Preprocessor directive to be able to use setprecision.
#include <fstream>             //Preprocessor directive to be able to output to file.

using namespace std;           //Uses standard namespace.

//Function prototypes
void showMenu();
float getFeet();
float getMiles();
float getYards();
float getFurlong();
void printOut(float);

int main ()                         //Main function

{
    int choice;                     //To hold menu choice

    //Constants for the menu choice
    const int FEET_CHOICE = 1,
              MILES_CHOICE = 2,
              YARDS_CHOICE = 3,
              FURLONG_CHOICE = 4,
              QUIT_CHOICE = 5;

    do
    {
        //Display menu and get user's choice
        showMenu();
        cin >> choice;

        //Validate menu selection
        while (choice < FEET_CHOICE || choice > QUIT_CHOICE)
        {
            cout << "Please entere a valid menu choice:";
            cin >> choice;
        }

        if (choice != QUIT_CHOICE)
        {
            switch(choice)
            {
                case FEET_CHOICE:
                    printOut(getFeet());
                    break;
                case MILES_CHOICE:
                    printOut(getMiles());
                    break;
                case YARDS_CHOICE:
                    printOut(getYards());
                    break;
                case FURLONG_CHOICE:
                    printOut(getFurlong());
                    break;
            }
        }
    } while (choice != QUIT_CHOICE);

    return 0;
}

//*************************************************************
// Definition of function showMenu which displays the menu.   *
//*************************************************************
void showMenu()
{
cout << "\n\t\tMenu\n\n"
     << "1. Input Feet\n"
     << "2. Input Miles\n"
     << "3. Input Yards\n"
     << "4. Input Furlong\n"
     << "5. Quit\n"
     << "Enter your choice: ";
}

//**************************************************************
// Definition of function getFeet which gets length and width. *
//**************************************************************
float getFeet()
{
float width;
float length;

cout << "\nPlease input width in feet: ";
cin >> width;

cout << "\nPlease input length in feet: ";
cin >> length;

fstream outputFile;
outputFile.open("LandAreaOutput.txt", ios::out | ios::app);

outputFile << "Width of " << width << " feet and length of " << length << " feet is ";

outputFile.close();

return (width*length);
}

//**************************************************************
// Definition of function getMiles which gets length and width.*
//**************************************************************
float getMiles()
{
float width;
float length;

cout << "\nPlease input width in miles: ";
cin >> width;

cout << "\nPlease input length in miles: ";
cin >> length;

fstream outputFile;
outputFile.open("LandAreaOutput.txt", ios::out | ios::app);

outputFile << "Width of " << width << " miles and length of " << length << " miles is ";

outputFile.close();

return (width*length*27878400);
}

//**************************************************************
// Definition of function getYards which gets length and width.*
//**************************************************************
float getYards()
{
float width;
float length;

cout << "\nPlease input width in yards: ";
cin >> width;

cout << "\nPlease input length in yards: ";
cin >> length;

fstream outputFile;
outputFile.open("LandAreaOutput.txt", ios::out | ios::app);

outputFile << "Width of " << width << " yards and length of " << length << " yards is ";

outputFile.close();

return (width*length*9);
}

//****************************************************************
// Definition of function getFurlong which gets length and width.*
//****************************************************************
float getFurlong()
{
float width;
float length;

cout << "\nPlease input width in furlongs: ";
cin >> width;

cout << "\nPlease input length in furlongs: ";
cin >> length;

fstream outputFile;
outputFile.open("LandAreaOutput.txt", ios::out | ios::app);

outputFile << "Width of " << width << " furlongs and length of " << length 
           << " furlongs is ";

outputFile.close();

return (width*length*435600);
}

//**************************************************************
// Definition of function feetOut which outputs conversions   .*
//**************************************************************
void printOut(float sqFeet)
{
fstream outputFile;
outputFile << fixed << showpoint;
outputFile << setprecision(2);
outputFile.open("LandAreaOutput.txt", ios::out | ios::app);

outputFile << sqFeet << " sq feet, " << sqFeet/43560 << " Eng acres, "
           << sqFeet/55321 << " Scot acres, " << sqFeet/70560 << " Irish acres, "
           << sqFeet/107639 << " Hectares, " << sqFeet/26910 << " Morgen (metric), "
           << sqFeet/5227200 << " Hides(std). " << "\n";

outputFile.close();

}

Source Code

Guess The Number

This is a problem from one of my classes called Guess the Number. We were to write a program that would generate a pseudo random number, and then let the user guess that number. The program required the following:

  • Allow the user to guess a number
  • Notify if the guessed number is high/low
  • Use of pseudo random number generator
  • Keep count of the number of guesses
  • Loop through 10 times and determine average number of guesses
  • Output for each game the average number of guesses made to guess the number
// *********************************************************************
// Description: Guess The Number
// *********************************************************************

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <fstream>

using namespace std;

int main()
{

int number;
int guess;
int sum = 0;
int game = 0;
char choice;

fstream outFile;

do
{
    int count = 0;
    game++;

    srand(time(NULL));
    number = rand() % 100 + 1;

    do
    {

    cout << "\nGuess an integer between 1-100: ";
    cin >> guess;
    cout << endl;

    count++;

        if (guess > number)
        {
            cout << guess << " is higher than the integer." << endl;
        }
        else if (guess < number)
        {
            cout << guess << " is lower than the integer." << endl;
        }
        else
        {
            cout << guess << " is the integer!" << endl;

            outFile.open("GuessTheNumberOutput.txt", ios::out | ios::app);
            outFile << number << " was the number, it took " << count 
                    << " guesses to get it right!\n";
            outFile.close();

        }

    } while (guess != number);

    cout << "\nDo you want to play another game (Y or N)? ";
    cin >> choice;

    sum += count;

} while (choice == 'Y' || choice == 'y');

outFile.open("GuessTheNumberOutput.txt", ios::out | ios::app);
outFile << "In " << game << " games it took an average of " << sum/float(game) 
        << " guesses to get the correct number." << endl;

return 0;

}

Source Code