Function Math

I have written this code for my class and the only two issues I have (big issues lol) are that the math performed in the function displays an output of 0.00 in my program and my loop will not end when I input 'y' or 'Y' to end it. Any advice would be greatly appreciated.

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<iostream>
#include<cmath>

const double FEET = 3.2808;
const double METER = 0.3048;

using namespace std;
 
void instruction();// Tells user how to operate program
void menu();//List the possible selection choices
void getFeet(double&);//Gets number of feet to converts to meters
void giveFeet(double, double);//Displays the converted output
void getMeters(double&);//Gets number of meters to converts to feet
void giveMeters(double, double);//Displays the converted output
void getSqFt(double&, double&);//Gets dimensions from user in feet to convert into meters
void giveSqFt(double, double, double, double, double, double);//Displays the square feet converted to meters
void getSqM(double&, double&);//Gets dimensions from user in meters to convert into feet
void giveSqM(double, double, double, double, double, double);//Displays the square meters converted to feet
double convertFt(double, double);//Conducts the conversion
double convertM(double, double);//Conducts the conversion
double conSqFt(double, double, double, double);//Conducts the conversion
double conSqM(double, double, double, double);//Conducts the conversion 
 
int main()
{
   instruction();
   int selection;
   char exitLoop;
   double amount, meters, conversion, quantity, feet, change, sqFt, lengthFt_M, lengthM_Ft;
   double lengthFt, widthFt, areaFt, areaM, lengthM, widthM, sqM, widthFt_M, widthM_Ft;
    
   do
   {
       do
       {
           menu();
           cin >> selection;
           selection = toupper (selection);
           if(selection < 1 || selection > 5)
               cout << "That is not a valid choice." <<endl;
       }
       while(selection != 1 && selection != 2 && selection != 3 && selection != 4 && selection != 5);
       if (selection == 1)
           {
               getFeet(amount);
               convertFt(amount, meters);
               giveFeet(amount, conversion);
           }
       else if (selection == 2)
           {
               getMeters(quantity);
               convertM(quantity, feet);
               giveMeters(quantity, change);
           }
       else if(selection == 3)
           {
               getSqFt(lengthFt, widthFt);
               conSqFt(lengthFt, widthFt, areaFt, meters);
               giveSqFt(lengthFt, widthFt, areaFt, areaM, lengthFt_M, widthFt_M);
           }
           
       else if (selection == 4)
           {
               getSqM(lengthM, widthM);
               conSqM(lengthM, widthM, sqM, feet);
               giveSqM(lengthM, widthM, sqM, sqFt, lengthM_Ft, widthM_Ft);
           }
           
       else if (selection == 5)
           {
               cout << "Are you sure you would like to exit this program? (Y = yes, N = no)" << endl;
               cin >> exitLoop; 
           }
   }
   while(exitLoop != 'Y' || exitLoop != 'y');                   
}
 
void instruction()
{
   cout << "This program allows users to convert measurements\n"
        << "from feet to meters and meters to feet. Please\n"
        << "select an option from the main menu.\n" 
        <<  "  "<< endl;
}
 
void menu()
{
   cout << "English-Metric Junior" << endl;
   cout << "1) Convert from feet to meters." << endl;
   cout << "2) Convert from meters to feet." << endl;
   cout << "3) Convert the area of a rectangle in square feet to the area in square meters." << endl;
   cout << "4) Convert the area of a rectangle in square meters to the area in square feet." << endl;
   cout << "5) Quit the program." << endl;
   cout << "Please enter the number that corresponds to the option you would like (1-5)." << endl;
}
 
void getFeet(double& amount)
{
   cout << "Enter the distance in feet you wish to convert." << endl;
   cin >> amount;
}

double convertFt(double amount, double meters)
{
    double amnt, METERS, resultFt;
    resultFt = amnt * METERS;
    return resultFt;
}
void giveFeet(double amount, double conversion)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << amount << " feet is equal to " << conversion << " meters!" << endl;
}

void getMeters(double& quantity)
{
    cout << "Enter the distance in meters you wish to convert." << endl;
    cin >> quantity;
}

double convertM(double quantity, double feet)
{
    double qty, FEET, resultM;
    resultM = qty * FEET;
    return resultM;
}

void giveMeters(double quantity, double change)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << quantity << " meters is equal to " << change << " feet!" << endl;
}

void getSqFt(double& lengthFt, double& widthFt)
{
  cout << "Enter the length in feet." << endl;
  cin >> lengthFt;
  cout << "Enter the width in feet." << endl;
  cin >> widthFt;  
}

double conSqFt(double lengthFt, double widthFt, double areaFt, double meters)
{
    double lFt, wFt, sqFt, METERS, resultSqFt;
    resultSqFt = ((lFt * METERS) * (wFt * METERS));
    return resultSqFt;
}

void giveSqFt(double lengthFt, double widthFt, double areaFt, double areaM,double lengthFt_M, double widthFt_M)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << "A length of " << lengthFt << " feet and width of " << widthFt << " feet produces an area of " << areaFt << " square feet."
         << "The converted length is " << lengthFt_M << " meters and the converted width is " << widthFt_M << " meters which produces an area of " << areaM << " square meters." << endl;
}

void getSqM(double& lengthM, double& widthM)
{
  cout << "Enter the length in meters." << endl;
  cin >> lengthM;
  cout << "Enter the width in meters." << endl;
  cin >> widthM;  
}

double conSqM(double lengthM, double widthM, double sqM, double feet)
{
    double lM, wM, resultSqM, FEET;
    resultSqM = ((lM * FEET) * (wM * FEET));
    return resultSqM;
}

void giveSqM(double lengthM, double widthM, double sqM, double sqFt, double lengthM_Ft, double widthM_Ft)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << "A length of " << lengthM << " meters and width of " << widthM << " meters produces an area of " << sqM << " square meters."
         << "The converted length is " << lengthM_Ft << " feet and the converted width is " << widthM_Ft << " feet which produces an area of " << sqFt << " square feet." << endl;
}

Line 28: You don't initialize exitLoop. The only place you set it is if selection == 5. For other selection, the value will be undefined.

Line 75: Your while condition is faulty. The condition will always be true. if exitLoop is 'y', then exitLoop != 'Y' will be true causing you to continue the loop, which is not what you want. You want an && condition here.

Line 46: You're inconsistent using parameters and return values. Why are you passing the second argument? You don't use it. convertFt returns the result, but again you don't use it. Similar problems exit with your other functions.

Line 47: conversion is unitialized, which you then try to output at line 144.
Last edited on
Im not really sure on what you mean by the parameters and return values...sorry, I am new to this whole programming thing and when my instructor was showing us how to do functions, he was using the different names for parameters and the return values because JGRASP kicks back an error saying value was already defined....idk? :/

also i fixed the loop with this:
while(exitLoop != 'Y' && exitLoop != 'y'&& exitLoop != 1 && exitLoop != 2 && exitLoop != 3 && exitLoop != 4 && exitLoop != 5);
Last edited on
Let's take a look at convertFt;
103
104
105
106
107
108
double convertFt(double amount, double meters)
{
    double amnt, METERS, resultFt;
    resultFt = amnt * METERS;
    return resultFt;
}

Line 103: Why are you passing meters? You don't use it in the function.

Line 106: You're using amnt in your calculation, but amnt is not initialized. You should be using amount (the parameter) in the calculation. amnt is not needed.

Line 107: You return resltFt as the result of the function. Now let's lookat where convertft is called:
45
46
47
        getFeet(amount);
        convertFt(amount, meters);
        giveFeet(amount, conversion);

You call convertFt at line 46, but you ignore the result of the function call. Your call should look like this:
1
2
    conversion = convertFt (amount, meters);  // Save result of function in conversion
    //  Ignoring the unused parameter (meters) 


Most of your other functions have similar issues.

edit:
Line 105: Why are you declaring METERS and then using the uninitialized value in your calculation? What you want here is the global constant METER.

convertFt should look like this:
103
104
105
double convertFt (double amount)
{  return amount * METER;
}





Last edited on
I am very appreciative of all of your help...i have corrected my program and it now works as it should. Here is the end result:
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<iostream>
#include<cmath>

const double FEET = 3.2808;
const double METER = 0.3048;

using namespace std;
 
void instruction();// Tells user how to operate program
void menu();//List the possible selection choices
void getFeet(double&);//Gets number of feet to converts to meters
void giveFeet(double, double);//Displays the converted output
void getMeters(double&);//Gets number of meters to converts to feet
void giveMeters(double, double);//Displays the converted output
void getSqFt(double&, double&);//Gets dimensions from user in feet to convert into meters
void giveSqFt(double, double, double, double, double, double);//Displays the square feet converted to meters
void getSqM(double&, double&);//Gets dimensions from user in meters to convert into feet
void giveSqM(double, double, double, double, double, double);//Displays the square meters converted to feet
double convertFt(double&, double);//Conducts the conversion
double convertM(double&, double);//Conducts the conversion
double SqFt(double&, double&, double&);//Gets the current units square root
double SqM(double&, double&, double&);//Gets the current units square root 
double conSqFt(double&, double&, double&);//Converts the square root units
double conSqM(double&, double&, double&);//Converts the square root units
 
int main()
{
   instruction();
   int selection;
   char exitLoop;
   double amount, conversion, quantity, change, squareFeet, squareMeter, lengthFt_M, lengthM_Ft;
   double lengthFt, widthFt, areaFt, areaM, lengthM, widthM, widthFt_M, widthM_Ft, convertSqM, convertSqFt;
   
   do
   {
       do
       {
           menu();
           cin >> selection;
           selection = toupper (selection);
           if(selection < 1 || selection > 5)
               cout << "That is not a valid choice." <<endl;
       }
       while(selection != 1 && selection != 2 && selection != 3 && selection != 4 && selection != 5);
       if (selection == 1)
           {
               getFeet(amount);
               conversion = convertFt(amount, METER);
               giveFeet(amount, conversion);
           }
       else if (selection == 2)
           {
               getMeters(quantity);
               change = convertM(quantity, FEET);
               giveMeters(quantity, change);
           }
       else if(selection == 3)
           {
               getSqFt(lengthFt, widthFt);
               squareFeet = SqFt(lengthFt, widthFt, squareFeet);
               convertSqFt = conSqFt(lengthFt, widthFt, convertSqFt); 
               giveSqFt(lengthFt, widthFt, squareFeet, convertSqFt, lengthFt_M, widthFt_M);
           }
           
       else if (selection == 4)
           {
               getSqM(lengthM, widthM);
               squareMeter = SqM(lengthM, widthM, squareMeter);
               convertSqM = conSqM(lengthM, widthM, convertSqM);
               giveSqM(lengthM, widthM, squareMeter, convertSqM, lengthM_Ft, widthM_Ft);
           }
           
       else if (selection == 5)
           {
               cout << "Are you sure you would like to exit this program? (Y = yes, N = no)" << endl;
               cin >> exitLoop; 
           }
   }
   while(exitLoop != 'Y' && exitLoop != 'y'&& exitLoop != 1 && exitLoop != 2 && exitLoop != 3 && exitLoop != 4 && exitLoop != 5);                   
}
 
void instruction()
{
   cout << "This program allows users to convert measurements\n"
        << "from feet to meters and meters to feet. Please\n"
        << "select an option from the main menu.\n" 
        <<  "  "<< endl;
}
 
void menu()
{
   cout << "English-Metric Junior" << endl;
   cout << " " << endl;
   cout << "1) Convert from feet to meters." << endl;
   cout << "2) Convert from meters to feet." << endl;
   cout << "3) Convert the area of a rectangle in square feet to the area in square meters." << endl;
   cout << "4) Convert the area of a rectangle in square meters to the area in square feet." << endl;
   cout << "5) Quit the program." << endl;
   cout << " " << endl;
   cout << "Please enter the number that corresponds to the option you would like (1-5)." << endl;
}
 
void getFeet(double& amount)
{
   cout << "Enter the distance in feet you wish to convert." << endl;
   cin >> amount;
}

double convertFt(double& amount, double METER)
{
    double cvn = amount * METER;
    return cvn;
    
}
void giveFeet(double amount, double conversion)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << amount << " feet is equal to " << conversion << " meters!" << endl;
}

void getMeters(double& quantity)
{
    cout << "Enter the distance in meters you wish to convert." << endl;
    cin >> quantity;
}

double convertM(double& quantity, double FEET)
{
    double cng = quantity * FEET;
    return cng;
}

void giveMeters(double quantity, double change)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << quantity << " meters is equal to " << change << " feet!" << endl;
}

void getSqFt(double& lengthFt, double& widthFt)
{
  cout << "Enter the length in feet." << endl;
  cin >> lengthFt;
  cout << "Enter the width in feet." << endl;
  cin >> widthFt;  
}

double SqFt(double& lengthFt, double& widthFt, double& squareFeet)
{
    double sqrFt = (lengthFt * widthFt);
    return sqrFt;
}

double conSqFt(double& lengthFt_M, double& widthFt_M, double& convertSqFt)
{
    double cnvtSqFt = ((lengthFt_M * METER) * (widthFt_M * METER));
    return cnvtSqFt;
}

void giveSqFt(double lengthFt, double widthFt, double squareFeet, double convertSqFt, double lengthFt_M, double widthFt_M)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << "A length of " << lengthFt << " feet and width of " << widthFt << " feet produces an area of " << squareFeet << " square feet.\n"
         << " \n" 
         << "The converted length is " << lengthFt * METER << " meters and the converted width is " << widthFt * METER << " meters which produces an area of " << convertSqFt << " square meters.\n" << endl;
}

void getSqM(double& lengthM, double& widthM)
{
  cout << "Enter the length in meters." << endl;
  cin >> lengthM;
  cout << "Enter the width in meters." << endl;
  cin >> widthM;  
}

double SqM(double& lengthM, double& widthM, double& squareMeter)
{
    double sqrM = (lengthM * widthM);
    return sqrM;
}

double conSqM(double& lengthM_Ft, double& widthM_Ft, double& convertSqM)
{
    double cnvtSqM = ((lengthM_Ft * FEET) * (widthM_Ft * FEET));
    return cnvtSqM;
}

void giveSqM(double lengthM, double widthM, double squareMeter, double convertSqM, double lengthFt_M, double widthM_Ft)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << "A length of " << lengthM << " meters and width of " << widthM << " meters produces an area of " << squareMeter << " square meters.\n"
         << " \n"
         << "The converted length is " << lengthM * FEET << " feet and the converted width is " << widthM * FEET << " feet which produces an area of " << convertSqM << " square feet.\n" << endl;
}
Last edited on
One general comment. You're using pass by reference a lot. While that works, in general you want to use pass by reference for simple values only when you intend to change the argument passed by the caller.

Passing by value is more commonly used when you don't intend for the function to change the caller's argument. You can also specify pass by reference arguments as const to indicate the the function must not change the caller's argument, but that is usually used when passing class or struct instances.
Topic archived. No new replies allowed.