I am new to programming and am having trouble grasping concepts with functions.
Here is my problem:
This program is to give the user the option of converting a set of temperatures either from Celsius to Fahrenheit (C to F) or vice versa, from Fahrenheit to Celsius (F to C), or to quit the program. If the user selects either C to F or F to C, the program will prompt the user to enter three integer values, a starting temperature, an ending temperature, and an increment. After these values have been entered the program will display a table of equivalent C and F (or F and C) temperatures, from the starting temperature to the ending temperature and incrementing by the increment value each row.
The table must meet all of the following criteria:
• The table's column headings should display the degree symbol, e.g., °C and °F.
• The first column must be the "from" temperature (C for C to F or F for F to C) and the second column the "to" temperature (F for C to F or C for F to C).
• The calculated "to" temperatures are to be displayed to the nearest tenth of a degree (display exactly one decimal place, even if there is no fractional part, i.e., 75° should display as 75.0°).
• Temperatures in both columns must be number-aligned (right-justified for the integer "from" values and decimal point aligned right for the "to" values).
• Assume the user enters correct data, e.g., the start temperature, end temperature and increment are all integers and the ending temperature is greater than the starting temperature.
here is what I have so far:
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
|
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
float CtoF(float);
float FtoC(float);
char displayMenu(); //displays a menu
char getMenuSelection(char); /*gets the menu selection from the user,
*upper or lower case 'C' for Celsius to
*Fahrenheit, upper or lowercase 'F' for
*Fahrenheit to Celsius, and upper or
*lowercase 'Q' to quit. Any other input should
*get an error message "Invalid selection: try again"
*and re-prompt for the menu selection.*/
float getStartEndAndIncrement(); //gets the start, end, and increment values for the table
void displayTable(); /*displays a C to F or F to C table given start, end, and
*increment values and the conversion character the user selected.*/
int main ()
{
char choice = displayMenu();
char menu = getMenuSelection ();
float degree = getStartEndAndIncrement ();
float fTemp, cTemp;
{
if (menu == 'f' || menu =='F')
{
cTemp = FtoC(degree);
fTemp = degree;
}
else if (menu == 'c' || menu == 'C')
{
fTemp = CtoF(degree);
cTemp = degree;
}
else
cout << "Invalid Selection: try again." << endl;
exit (EXIT_FAILURE);
}
float startDeg, endDeg, increment;
getStartEndAndIncrement(startDeg, endDeg, increment);
displayTable(startDeg, endDeg, increment);
cin.ignore(2);
return 0;
}
char getMenuSelection(char choice)
{
switch (choice)
{
case 'c':
case 'C':
{
cout << "Enter temperature
float getStartEndAndIncrement ()
{
float startDeg;
cout << "Enter starting temperature reading in degrees: " << endl;
cin >> startDeg;
float endDeg;
cout << "Enter the ending temperature reading in degrees: " << endl;
cin >> endDeg;
float increment;
cout << "Enter the increment for the displayed temperatures you want to see: " << endl;
cin>> increment;
return endDeg;
return increment;
return startDeg;
}
float FtoC (float fTemp)
{
float cTemp;
cTemp = (5. * (fTemp - 32.)) / 9.;
return cTemp;
}
float CtoF (float cTemp)
{
float fTemp;
fTemp = (( 9. * cTemp) / 5.) + 32.;
return fTemp;
}
void displayTable (float startDeg, float endDeg, float increment)
{
cout << "Celsius" << "\t" << "Fahrenheit" << endl;
cout << "
|
for (float i = startDeg; i < end; i += increment)
{
cout << i << "\t" << CtoF(i) << endl;
}
}
int displayMenu(int choice)
{
cout << "MENU" << endl;
cout << " C : Convert Fahrenheit to Celsius" << endl;
cout << " F : Convert Celsius to Fahrneheit" << endl;
cout << " Q : Quit program" << endl;
cout << "Enter your choice: ";
char choice;
cin >> choice;
return choice;
} |
I think my biggest issue is how to organize all the functions, I know I have to create a function which gives the user three options, then I have to have another function which calls that functions' 'answer' and that's kind of where I am lost. I also realize that my table is currently setup incorrectly, I am working on getting that to adjust dependent on users' request as the 'from' temp must be on the right...
Only looking for guidance, not answers please and THANK YOU!