Create a C++ program to determine how much cloth in square inches is needed to m
ID: 3783090 • Letter: C
Question
Create a C++ program to determine how much cloth in square inches is needed to make a certain type of garment. You should base your calculations on the following table:
Rather than putting all your code in main(), use functions to perform the calculation. Embed your program in a loop so that the calculation can be repeated multiple times. Use constants to hold the literal values in the above table, for example:
const double PANTS_WAIST_FACTOR = 2 + 1.0/2;
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: P
Gimme your waist size in inches: 30
Gimme your height size in inches: 72
Pleaded front? [Y/N]: Y
Baggy Look? [Y/N]: N
For your pants, you'll need 114 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: S
Gimme your waist size in inches: 32
Gimme your height size in inches: 50
Long sleeves? [Y/N]: Y
Gimme your arms length in inches: 25
For your shirts, you'll need 168.2222 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: T
Gimme your waist size in inches: 35
Gimme your height size in inches: 72
Pockets? [Y/N]: Y
For your shorts, you'll need 73.025 square inches of fabric!
Try again? [Y/N]: N
Explanation / Answer
// C++ code
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
const double PANTS_WAIST_FACTOR = 2 + 1.0/2;
const double PANTS_HEIGHT_FACTOR = 1.0/2;
const double PANTS_PLEADED_FACTOR = 1.0/10;
const double PANTS_BAGGY_FACTOR = 0.1;
const double SHIRTS_WAIST_FACTOR = 2 + 3.0/8;
const double SHIRTS_HEIGHT_FACTOR = 4.0/9;
const double SHIRTS_LONG_SLEEVES_FACTOR = 2 + 4.0/5;
const double SHORTS_WAIST_FACTOR = 1 + 3.0/10;
const double SHORTS_HEIGHT_FACTOR = 1.0/4;
const double SHORTS_POCKET_FACTOR = 0.15;
char choice,again;
double waist_size, height_size, fabric_required, arms_length;
char pleaded_front, baggy_look, long_sleeves, pockets;
while(true)
{
cout << " Tailor Fabric Calculator: ";
cout << "Whaddya want? [P]ants or [S]hirts or shor[T]s: ";
cin >> choice;
if(choice == 'P')
{
cout << "Gimme your waist size in inches: ";
cin >> waist_size;
cout << "Gimme your height size in inches: ";
cin >> height_size;
cout << "Pleaded front? [Y/N]: ";
cin >> pleaded_front;
cout << "Baggy Look? [Y/N]: ";
cin >> baggy_look;
fabric_required = waist_size*PANTS_WAIST_FACTOR + height_size*PANTS_HEIGHT_FACTOR;
if(pleaded_front == 'Y')
fabric_required = fabric_required + waist_size*PANTS_PLEADED_FACTOR;
if(baggy_look == 'Y')
fabric_required = fabric_required + fabric_required*0.15;
cout << "For your pants, you'll need " << fabric_required << " square inches of fabric!" << endl;
}
else if(choice == 'S')
{
cout << "Gimme your waist size in inches: ";
cin >> waist_size;
cout << "Gimme your height size in inches: ";
cin >> height_size;
fabric_required = waist_size*SHORTS_WAIST_FACTOR + height_size*SHIRTS_HEIGHT_FACTOR;
cout << "Long sleeves? [Y/N]: ";
cin >>long_sleeves;
if(long_sleeves == 'Y')
{
cout << "Gimme your arms length in inches: ";
cin >> arms_length;
fabric_required = fabric_required + arms_length*SHIRTS_LONG_SLEEVES_FACTOR;
}
cout << "For your shirts, you'll need " << fabric_required << " square inches of fabric!" << endl;
}
else if(choice == 'T')
{
cout << "Gimme your waist size in inches: ";
cin >> waist_size;
cout << "Gimme your height size in inches: ";
cin >> height_size;
cout << "Pockets? [Y/N]: ";
cin >> pockets;
fabric_required = waist_size*SHORTS_WAIST_FACTOR + height_size*SHORTS_HEIGHT_FACTOR;
if(pockets == 'Y')
fabric_required = fabric_required + fabric_required*SHORTS_POCKET_FACTOR;
cout << "For your shorts, you'll need " << fabric_required << " square inches of fabric!" << endl;
}
cout << "Try again? [Y/N]: ";
cin >> again;
if(again == 'N')
break;
}
return 0;
}
/*
output:
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: P
Gimme your waist size in inches: 30
Gimme your height size in inches: 72
Pleaded front? [Y/N]: Y
Baggy Look? [Y/N]: N
For your pants, you'll need 114 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: S
Gimme your waist size in inches: 32
Gimme your height size in inches: 50
Long sleeves? [Y/N]: Y
Gimme your arms length in inches: 25
For your shirts, you'll need 133.822 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: T
Gimme your waist size in inches: 35
Gimme your height size in inches: 72
Pockets? [Y/N]: Y
For your shorts, you'll need 73.025 square inches of fabric!
Try again? [Y/N]: N
*/