1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
//Author:
//Date: 04/10/2013
//Title: Temperature Scale Convertor
//Description: User selects the conversion from a menu then inputs value to be converted, converted value is outputted
#include <iostream>
#include <cstdlib>//header to allow program to use clear screen command
using namespace std;
int menu (int &selection);//prototype for menu function
void celsius_input (float &celsius_value);//prototype to get user to input temperature in Celsius
void fahrenheit_input (float &fahrenheit_value);//prototype to get user to input temperature in Fahrenheit
float convert_to_fahrenheit (float celsius_value, float celsius_converted);//prototype for converting from Celsius to Fahrenheit
float convert_to_celsius (float fahrenheit_value, float fahrenheit_converted);//prototype for converting from Fahrenheit to Celsius
void celsius_output (float fahrenheit_value, float fahrenheit_converted);//prototype that outputs converted Fahrenheit temperature in Celsius
void fahrenheit_output (float celsius_value, float celsius_converted);//prototype that outputs converted Celsius temperature in Fahrenheit
int main ()
{
float celsius_value;//declaring variable of type float to hold temperature in Celsius
float fahrenheit_value;//declaring variable of type float to hold temperature in Fahrenheit
float celsius_converted;//declaring variable of type float to hold value of temperature in Fahreheit after the Celsius Temperature has been converted
float fahrenheit_converted;//declaring variable of type float to hold value of temperature in Celsius after the Fahrenheit Temperature has been converted
int selection = 0;//initialising variable of type int to hold menu choice inputted by user. Value of 0 assigned to ensure menu in menu function is displayed at least once
menu (selection);//calling function to display menu and get user's selection
//cout << "\nSelection value is: " << selection;//used in unit testing menu function - commented out once unit testing completed
//introducing 2nd unit to be tested - switch statement
//unit testing conducted 05/10/13
//used cout statements to verify that that the correct case was selected
//test data used = 1, 2 & 3
//compiled with no syntax errors
//unit testing completed 05/10/13 - programme working as intended, i.e. correct switch case being selected using value passed out of menu function
//uses value from selection to select the correct switch case
switch(selection)
{
case 1 : //cout << "Option 1 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
celsius_input (celsius_value);//calling celsius_imput function to get value from user
//cout << "\nCelsius Temperature is: " << celsius_value;//used in unit testing celsius_input function - commented out once unit testing completed
fahrenheit_output (celsius_value, celsius_converted);//calling fahrenheit_output function to output converted Celsius temperature in Fahrenheit
break;//to stop fall through
case 2 : //cout << "Option 2 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
fahrenheit_input (fahrenheit_value);//calling fahrenheit_imput function to get value from user
//cout << "\nFahrenheit Temperature is: " << fahrenheit_value;//used in unit testing fahrenheit_input function - commented out once unit testing completed
celsius_output (fahrenheit_value, fahrenheit_converted);//calling fahrenheit_output function to output converted Celsius temperature in Fahrenheit
break;//to stop fall through
case 3 : system("CLS");//clears screen
cout << "Exit chosen from menu. Programme terminating." << endl;
//cout << "Option 3 chosen from menu";//used in unit testing switch statement - commented out once unit testing completed
break;//to stop fall through
//default can't be reached due to restrictions on selection variable
default : cout << "\n Invalid selection";
//no break required in the default case
}
int x;
cin >> x;
return 0;
}
int menu (int &selection)
{
while (selection < 1 || selection >3)//while loop used to keep displaying menu until user enters a valid menu choice
{
system("CLS");//clears screen
//displays menu to screen
cout << "Menu" << endl;
cout << "=======" << endl;
cout << "(1)\tConvert Celsius to Fahrenheit" << endl;
cout << "(2)\tConvert Fahrenheit to Celsius" << endl;
cout << "(3)\tExit" << endl;
cout << "\nEnter choice from menu: ";//user prompted to select from menu
//read input from user
cin >> selection;//selection variable initialised using value inputted by user
cin.clear();//clears error state of stream
cin.ignore(100, '\n');//discard characters remaining in the input buffer
}
}
void celsius_input (float &celsius_value)
{
system("CLS");//clears screen
cout << "Please enter your temperature in Celsius: ";//prompt user to input temperature value in Celsius
cin >> celsius_value;//celsius_value variable initialised using user's input
}
float convert_to_fahrenheit (float celsius_value, float celsius_converted)
{
celsius_converted = ((9*celsius_value)/5) + 32;
return celsius_converted;
}
void fahrenheit_output (float celsius_value, float celsius_converted)
{
cout << "\nTemperature in Fahrenheit is: " << convert_to_fahrenheit (celsius_value, celsius_converted) << endl;
}
void fahrenheit_input (float &fahrenheit_value)
{
system("CLS");//clears screen
cout << "Please enter your temperature in Fahrenheit: ";//prompt user to input temperature value in Fahrenheit
cin >> fahrenheit_value;//fahrenheit_value variable initialised using user's input
}
float convert_to_celsius (float fahrenheit_value, float fahrenheit_converted)
{
fahrenheit_converted = (((fahrenheit_value - 32)/9) * 5);
return fahrenheit_converted;
}
void celsius_output (float fahrenheit_value, float fahrenheit_converted)
{
cout << "\nTemperature in Celsius is: " << convert_to_celsius (fahrenheit_value, fahrenheit_converted) << endl;
}
|