~~DISCUSSION|C++ Demo Code Discussion~~
This is the home of the **C++ Demonstration Code**. The syntax and meaning of this code are not described here, as its context will be explained through other demonstrations. This page is only meant to be a repository for gathering the code in its original form. Further documentation can be found on the [[notes:projects:cpp|C++page]].
=====Version 1: if...else =====
[[http://lab46.corning-cc.edu/~mcooper6/downloads/cpp/converter.cc|converter.cc]]
/*
*
*converter.cc
*
*/
#include
#include
#include
#include
using namespace std;
// A menu driven program that allows the user to choose an option
// and then performs a task based on the option chosen
int main(){
int option(0);
do{
cout << "\n\n\n\n\n\n\n";
cout << " 1. Fahrenheit to Celcius" << endl;
cout << " 2. Celcius to Fahrenheit" << endl;
cout << " 3. Inches to centimeter" << endl;
cout << " 4. Centimeters to inches" << endl;
cout << " 5. Pounds to kilograms" << endl;
cout << " 6. Kilograms to pounds" << endl;
cout << " 0. Exit" << "\n\n";
cout << "\n\n\n";
cout << " Choose an option: ";
cin >> option;
cout << "\n\n\n\n\n\n\n";
if (option == 1){
//100c is equal to 212f AND
//0 c is equal to 32f THEREFORE
//the ratio of fahrenheit to celcius is (212-32)/(100-0) THEREFORE
//fahrenheit = (9/5)*c+32
//celcius = (5/9)*(f-32)
//since we're after the modifier 1.81 AND NOT 1 => use 9.0/5.0
double f(0); //temp in Fahrenheit
double c(0); //temp in Celcius
//Get fahrenheit from the user
cout << "Enter the temerature in Fahrenheit: ";
cin >> f;
c=(5.0/9.0)*(f-32);
cout << f << " degrees Fahrenheit is " << fixed << showpoint << setprecision(1) << c << " degrees Celcius.\n\n";
}else if (option == 2){
//100c is equal to 212f AND
//0 c is equal to 32f THEREFORE
//the ratio of fahrenheit to celcius is (212-32)/(100-0) THEREFORE
//fahrenheit = (9/5)*c+32
//celcius = (5/9)*(f-32)
//since we're after the modifier 1.81 AND NOT 1 => use 9.0/5.0
double f(0);
double c(0);
//get celcius from the user
cout << "Enter a temperature in Celcius: ";
cin >> c;
f=(9.0/5.0)*c+32;
cout << c << " degrees celcius is " << fixed << showpoint << setprecision(1) << f << " degrees fahrenheit.\n\n";
}else if (option == 3){
// inches to centimeters
const double IN_CONV = 2.54;
int inches(0);
cout << "Please enter a length in inches:";
cin >> inches;
double cent(inches*IN_CONV);
cout << inches << " inches is " << showpoint << setprecision(2) << cent << " centimeters\n";
}else if (option == 4){
//centimeters to inches
const double IN_CONV = 2.54;
double cent(0);
cout << "Please enter a length in centimeters:";
cin >> cent;
double inches(cent/IN_CONV);
cout << cent << " centimeters is " << showpoint << setprecision(2) << inches << " Inch(es)\n";
}else if (option == 5){
// pounds to kilos
double p(0); //pounds
double k(0); //kilos
cout << " Please enter a weight in pounds: ";
cin >> p;
k = p/2.2;
cout << p << " pounds is equal to " << setprecision(2) << k << " kilograms.\n\n";
}else if (option == 6){
// kilos to pounds
double p(0); //pounds
double k(0); //kilos
cout << " Please enter a weight in kilograms: ";
cin >> k;
p = k*2.2;
cout << k << " kilograms is equal to " << setprecision(2) << p << " pounds.\n\n";
}
}while (option>0 && option<7);
cout << "\n\n\n\n\n\n\n";
}
===== Version 2: Modular =====
this modularizes the program but as long as the files are in the same directory and you use "" instead of <> on your includes it will work as two files. its a little bit different than the example above using functions and a switch statement to clean up the main function a bit but it work the same way. in this example there is three files instead of one. the main program in conv.cpp and that calls funtions from two other files menu.H and functions.H. splitting a program up like this can be beneficial if you want to have a main functions file that all your programs can use or if your programs begin to get long you can break them up for ease of reading and troubleshooting.
[[http://lab46.corning-cc.edu/~mcooper6/downloads/cpp/menu.H|menu.H]]
/*
*menu.H - menu for conv.cpp file.
*
* use by: #include"menu.H"
* call by: menu();
*/
#include
using namespace std;
void menu()
{
cout << " 1. Fahrenheit to Celcius" << endl;
cout << " 2. Celcius to Fahrenheit" << endl;
cout << " 3. Inches to centimeter" << endl;
cout << " 4. Centimeters to inches" << endl;
cout << " 5. Pounds to kilograms" << endl;
cout << " 6. Kilograms to pounds" << endl;
cout << " 0. Exit" << "\n\n";
}
[[http://lab46.corning-cc.edu/~mcooper6/downloads/cpp/functions.H|functions.H]]
/*
*
* functions.H - functions for the conv.cpp program
*
* use by: include"functions.H"
* call by: function name below
*
*/
#include
#include
using namespace std;
void fahr()
{
double f(0); //temp in Fahrenheit
double c(0); //temp in Celcius
//Get fahrenheit from the user
cout << "Enter the temerature in Fahrenheit: ";
cin >> f;
c=(5.0/9.0)*(f-32);
cout << f << " degrees Fahrenheit is " << fixed << showpoint <<
setprecision(1) << c << " degrees Celcius.\n";
}
void celc()
{
double f(0);
double c(0);
//get celcius from the user
cout << "Enter a temperature in Celcius: ";
cin >> c;
f=(9.0/5.0)*c+32;
cout << c << " degrees celcius is " << fixed << showpoint <<
setprecision(1) << f << " degrees fahrenheit.\n\n";
}
void cent()
{
// inches to centimeters
const double IN_CONV = 2.54;
int inches(0);
cout << "Please enter a length in inches:";
cin >> inches;
double cent(inches*IN_CONV);
cout << inches << " inches is " << showpoint <<
setprecision(2) << cent << " centimeters\n";
}
void inch()
{
//centimeters to inches
const double IN_CONV = 2.54;
double cent(0);
cout << "Please enter a length in centimeters:";
cin >> cent;
double inches(cent/IN_CONV);
cout << cent << " centimeters is " << showpoint <<
setprecision(2) << inches << " Inch(es)\n";
}
void kilo()
{
// pounds to kilos
double p(0); //pounds
double k(0); //kilos
cout << " Please enter a weight in pounds: ";
cin >> p;
k = p/2.2;
cout << p << " pounds is equal to " << setprecision(2) <<
k << " kilograms.\n\n";
}
void pound()
{
// kilos to pounds
double p(0); //pounds
double k(0); //kilos
cout << " Please enter a weight in kilograms: ";
cin >> k;
p = k*2.2;
cout << k << " kilograms is equal to " << setprecision(2) <<
p << " pounds.\n\n";
}
void lines()
{
cout << "\n\n";
}
[[http://lab46.corning-cc.edu/~mcooper6/downloads/cpp/conv.cpp|conv.cpp]]
/*
* conv.cpp - this is a menu drivent program that allows the user to choose an option
* and then perform a task based on the option chosen
*
* requires: menu.H, functions.H (to be in current dir)
* compile: g++ conv.cpp -o conv
* execute: ./conv
*
*/
#include
#include
#include"menu.H"
#include"functions.H"
using namespace std;
int main()
{
int option(0);
do{
lines();
menu();
cout << " Choose an option: ";
cin >> option;
lines();
switch(option)
{
case 1:
fahr();
break;
case 2:
celc();
break;
case 3:
cent();
break;
case 4:
inch();
break;
case 5:
kilo();
break;
case 6:
pound();
break;
default:
break;
}
}while (option>0 && option<7);
lines();
return(0);
}
requires: menu.H, functions.H (to be in current dir) \\
instead of having to use "" u still can use <> and dont have to worry about having files in dir
if you have added the files to /usr/include/ dir in lab46.. that way everyone can use them!
=====Version 3: Classes=====
Version 3 is not actually the same program as versions 1, 2, 4 or 5, however it exhibits some behaviors that the final Version 3 hopes to have.
/*
* classDay.cc - class for the day program.
*/
#include
using namespace std;
class dayType
{
public:
void setDay();
string printDay();
string returnDay(int value);
void returnNDay();
void returnPDay();
void dayCalc();
dayType();
private:
string day;
int dayValue;
};
/*
* funcDay.cc - functions for the day program
*
* uses - classDay
*/
#include
#include
#include"classDay.cc"
using namespace std;
void dayType::setDay()
{
string inDay;
cout << "Enter a day of the week: ";
cin >> inDay;
if (inDay == "monday" || inDay == "mon")
{
day = "monday";
dayValue = 1;
}
else if (inDay == "teusday" || inDay == "teu")
{
day = "tuesday";
dayValue = 2;
}
else if (inDay == "wednesday" || inDay == "wed")
{
day = "wednesday";
dayValue = 3;
}
else if (inDay == "thursday" || inDay == "thu")
{
day = "thursday";
dayValue = 4;
}
else if (inDay == "friday" || inDay == "fri")
{
day = "friday";
dayValue = 5;
}
else if (inDay == "saterday" || inDay == "sat")
{
day = "saterday";
dayValue = 6;
}
else if (inDay == "sunday" || inDay == "sun")
{
day = "sunday";
dayValue = 7;
}
else
cout << "Not a day of the week!" << endl;
}
string dayType::printDay()
{
string rDay;
rDay = day;
return rDay;
}
string dayType::returnDay(int value)
{
string rDay;
switch(value)
{
case 1:
rDay = "monday";
break;
case 2:
rDay = "teusday";
break;
case 3:
rDay = "wednesday";
break;
case 4:
rDay = "thursday";
break;
case 5:
rDay = "friday";
break;
case 6:
rDay = "saterday";
break;
case 7:
rDay = "sunday";
default:
rDay = "error";
}
return rDay;
}
void dayType::returnNDay()
{
string rDay;
if (dayValue > 6)
cout << "The next day is monday" << endl;
else
{
rDay = returnDay(dayValue + 1);
cout << "The next day is " << rDay << endl;
}
}
void dayType::returnPDay()
{
string rDay;
if (dayValue < 2)
cout << "The previous day is sunday" << endl;
else
{
rDay = returnDay(dayValue - 1);
cout << "The previous day is " << rDay << endl;
}
}
void dayType::dayCalc()
{
string rDay;
int value;
int newValue;
cout << "how many days do you want add? ";
cin >> value;
cout << endl;
newValue = value;
while (newValue > 7)
{
newValue -= 7;
}
cout << "In " << value << " days the day will be "
<< returnDay(dayValue + newValue) << endl;
}
dayType::dayType()
{
day = "monday";
dayValue = 1;
}
/*
* day.cc - main program uses classDay and funcDay
*
* requires - classDay.cc and funcDay.cc
* compile - g++ day.cc -o day
* to run - ./day
*/
#include
#include
#include"classDay.cc"
void menu(dayType curDay);
int main()
{
char end = 'y';
int option;
dayType curDay;
while (end != 'n')
{
menu(curDay);
cin >> option;
cout << endl;
switch (option)
{
case 1:
curDay.setDay();
break;
case 2:
cout << curDay.printDay() << endl;
break;
case 3:
curDay.returnNDay();
break;
case 4:
curDay.returnPDay();
break;
case 5:
curDay.dayCalc();
break;
default:
cout << "Not an option!" << endl;
}
cout << "do you want to see the menu again?(y or n): ";
cin >> end;
}
return 0;
}
void menu(dayType curDay)
{
cout << "****Day Finder Program****" << endl;
cout << " The current day is: " << curDay.printDay() << endl << endl;
cout << " (1) set the day" << endl;
cout << " (2) print the day" << endl;
cout << " (3) find next day" << endl;
cout << " (4) find previous day" << endl;
cout << " (5) day calculator" << endl << endl;
cout << "what would you like to do: ";
}
=====Version 4: Using Make=====
Coming soon! an example that require Makefile!!