### How to define function...passing values?

I am writing a program that executes two operations on a structure distanceType which is compose of the int members: feet, yards, and miles.The function convertYards takes a distance in yards that is greater than or equal to 1760 (because there are 1760 yards in a mile) and converts it to x miles, y yards, and z feet (into distanceType). I'm having trouble with the parameters. It takes in an integer value and returns a distanceType. The problem is I am not sure how to define the function, because it's supposed to accept either distance1 or distance2. In my definition: distanceType convertYards( int ---) what am I supposed to put in place of the "---"?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165`` ``````int main() { char choice; char type1, type2; int distance1, distance2; int time; distanceType var1, var2; do { cout << "Enter A to add two distances, B to calculate miles per hour (or EXIT to exit):" << endl; cin >> choice; if (choice=='A'||choice=='a') { cout << "Enter distance type for first distance (M for miles, Y for yards, or F for feet):" << endl; cin >> type1; cout << "Enter distance type for second distance (M for miles, T for yards, or F for feet):" << endl; cin >> type2; switch (type1) { case 'M': case 'm': { cout << "Enter distance in miles:" << endl; cin >> var1.miles; break; } case 'Y': case 'y': { cout << "Enter distance in yards:" << endl; cin >> distance1; if (distance1 < 1760) { var1.yards = distance1; } else if (distance1 >= 1760) { convertYards(distance1); } break; } case 'F': case 'f': { cout << "Enter distance in feet:" << endl; cin >> distance1; if (distance1 < 3) { var1.feet = distance1; } else if (distance1 >= 3) { convertFeet (distance1); } break; } default: cout << "Invalid distance type" << endl; } switch (type2) { case 'M': case 'm': { cout << "Enter distance in miles:" << endl; cin >> var2.miles; break; } case 'Y': case 'y': { cout << "Enter distance in yards:" << endl; cin >> distance2; if (distance2 < 1760) { var2.yards = distance2; } else if (distance2 >= 1760) { convertYards(distance2); } break; } case 'F': case 'f': { cout << "Enter distance in feet:" << endl; cin >> distance2; if (distance2 < 3) { var2.feet = distance2; } else if (distance2 >= 3) { convertFeet(distance2); } break; } default: cout << "Invalid distance type" << endl; } addDistance(var1, var2); } else if(choice=='B'||choice=='b') { cout << "Enter distance type (M for miles, Y for yards, or F for feet):" << endl; cin >> type1; cout << "Enter a time (in minutes):" << endl; cin >> time; switch (type1) { case 'M': case 'm': { cout << "Enter distance in miles:" << endl; cin >> var1.miles; break; } case 'Y': case 'y': { cout << "Enter distance in yards:" << endl; cin >> distance1; if (distance1 < 1760) { var1.yards = distance1; } else if (distance1 >= 1760) { convertYards(distance1); } break; } case 'F': case 'f': { cout << "Enter distance in feet:" << endl; cin >> distance1; if (distance1 < 3) { var1.feet = distance1; } else if (distance1 >= 3) { convertFeet(distance1); } break; } default: cout << "Invalid distance type" << endl; } calcMPH(var1, time); } } while (choice != "EXIT"); } struct distanceType { int miles; int yards; int feet; } distanceType convertYards(int {``````
In my opinion, most programmers can't write a complete program then put in their calculations and have it work the first time. Do the math portion first, Then do the output section, and you'll know what the user input variables should be. T

In convertYards you want to get the value from another function, so you can pass the value or make a global value.

In this case, when the user enters a distance, I would convert that to feet. Pass feet to convertYards.

Once you have the distance in feet, it's easy to convert to yards or miles.
Otherwise, if you add distance 1 and 2, and the values are 59, 59, 59 you have a hard time converting 118 miles 118 yards and 118 feet into anything.
if A add the distances and then convert
if B 60 miles per hour = 88 feet per second.

To me it looks like you have a lot of things to fix in your code.

If I was doing this, I would start simple, get the math working, get the output looking like I wanted, and then add the user input.

Sorry i can't help but play with examples when I'm thinking about a problem. Hope this is helpful.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include #include #include #include using namespace std; int yards=1760; int feet=5280; int UserInputFeet=5287; int TimeinMins=60; int x=0; // Miles int y=0; // Yards int r=0; // Remainder void convert() { x=UserInputFeet/feet; // Miles traveled r=(UserInputFeet-(x*feet)); y=r/3; // Yards r=(UserInputFeet-(x*feet)); r=(r-(y*3)); // Feet } void Report() { cout << " Miles Traveled " << x << endl; cout << " Yards Traveled " << y << endl; cout << " Feet Traveled " << r << endl; // Feet traveled cout << " User traveled " << x << " Miles " << y << " Yards " << r << " Feet in " << (TimeinMins/60) << " hour" << endl; cout << " User traveled " << UserInputFeet << " Feet in " << TimeinMins << " Mins" << endl; cout << " User traveled " << UserInputFeet << " Feet in " << (TimeinMins*60) << " Seconds" << endl; } int main() { convert (); Report(); return 0; } ``````

Last edited on
Topic archived. No new replies allowed.