Hi, I'm having trouble using value returning functions with my code. When I input 8 for the mileage, the answer should be 506880, yet when I run it, it outputs 4201232. And I am also getting the wrong circumference for my second value returning function. I'm doing something wrong, but I'm not sure what. Any help would be greatly appreciated. Here's what I have so far.
#include <iostream>
usingnamespace std;
//function to convert miles to inches; return inches
int miles_to_inches (int miles) {
int inches;
inches = miles * 63360;
return inches;
}
//function that takes radius and returns circumference
double radius_circumference (int radius) {
double circumference;
circumference = 2 * 3.14 * radius;
return circumference;
}
//function that takes circumference and length, in inches,
//of a one-way trip to work and returns number of trips
//before tire wears out 25%
int num_trips (int circumference, int trip_inches) {
}
int main ()
{
int miles, inches, tireRadius;
double circumference;
cout << "How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? ";
cin >> miles;
cout << endl;
cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? ";
cin >> tireRadius;
cout << endl;
cout << "The distance you entered, in inches, is " << inches;
cout << endl;
cout << "The circumference of the tire you selected is is " << circumference;
cout << endl;
the values you are seeing are uninitialised values, you never call the functions
you have some misunderstanding of functions and scope
the `inches' in `miles_to_inches()' have no relationship with the `inches' in `main()'
think of functions like black boxes, you put some parameters, you receive a value, period
for example cout << "1 mile = " << miles_to_inches(1) << " inches\n";
miles_to_inches(1) will call the function, there miles is assigned the value 1, performs the computation and returns 63360, which is passed to `cout' and printed
int main ()
{
int miles, inches, tireRadius;
double circumference;
cout << "How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? ";
cin >> miles;
cout << endl;
cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? ";
cin >> tireRadius;
cout << endl;
inches = miles_to_inches(miles);
circumference = radius_circumference(tireRadius);
cout << "The distance you entered, in inches, is " << inches;
cout << endl;
cout << "The circumference of the tire you selected is is " << circumference;
cout << endl;
}
you are getting random junk because you did not initialize your variables and you did not call your functions. in main, you just say int inches; //other stuff... cout << inches; //junk value
try putting
inches = miles_to_inches(miles);
on line 42
you don't NEED inches variable, you can also say
cout << "The distance you entered, in inches, is " << miles_to_inches(miles);
I have now changed it to what ne555 was recommending. I see now that they were completely separate, which is why I was getting random numbers. But when I changed it, I receive an error. The error is: "expression preceding parentheses of apparent call must have (pointer-to-) function type "
#include <iostream>
using namespace std;
int main ()
{
int miles, inches, tireRadius, miles_to_inches;
double circumference, radius_circumference;
cout << "How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? ";
cin >> miles;
cout << endl;
cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? ";
cin >> tireRadius;
cout << endl;
cout << "The distance you entered, in inches, is " << inches;
cout << endl;
cout << "The circumference of the tire you selected is is " << circumference;
cout << endl;
#include <iostream>
usingnamespace std;
constexprdouble PI{ 3.141592653589793238 }; // <--- A more accurate value of PI, but not 100%.
constexpt int INCHES_PER_MILE{ 63360 };
//function to convert miles to inches; return inches
int miles_to_inches(int miles)
{
int inches;
inches = miles * INCHES_PER_MILE; //<--- Should use a constant variable.
return inches;
}
//function that takes radius and returns circumference
double radius_circumference(int radius)
{
double circumference;
circumference = 2 * PI * radius; //<--- Should use a constant variable.
return circumference;
}
//function that takes circumference and length, in inches,
//of a one-way trip to work and returns number of trips
//before tire wears out 25%
int num_trips(int circumference, int trip_inches)
{
return 0; // <--- Needed for compile. Will need to be changed later.
}
int main()
{
int miles{}, inches{}, tireRadius{}/*, miles_to_inches*/; // <--- Can not define a variable the same name as a function.
double circumference{}/*, radius_circumference*/; // <--- Can not define a variable the same name as a function.
cout << "How far is a one-way trip in miles from your house to the Slate Rock and Gravel Quarry? ";
cin >> miles;
cout << endl;
cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? ";
cin >> tireRadius;
cout << endl;
inches = miles_to_inches(miles);
circumference = radius_circumference(tireRadius);
cout << "The distance you entered, in inches, is " << inches;
cout << endl;
cout << "The circumference of the tire you selected is is " << circumference;
cout << endl;
return 0; // <--- Not required, but makes a good break point.
}
In lines 34 and 35 defining the variables "miles_to_inches" and "radius_circumference" These variables are local to "main" and overshadow the functions of the same name. That is why you are getting the errors. The compiler thinks line 45 and 46 lhs of = to the variables on the rhs which you are trying to use a function call. This does not work. Just because your functions are at the global scope of the file does not mean they are accessible everywhere as you see in "main" when you define a variable of the same name.
Removing the variable names in "main" allows you to use the functions of the same name.
In the future when you have an error message post the complete message and not what you may think it means. Chances are that you may be wrong in what you are thinking.
I do have a question about: cout << "What size tires would you like to purchase (12, 14, or 16-inch radius)? "; mostly the part in bold. From what I know and have found the 12, 14 and 16 refer to the diameter of the wheel not the overall diameter of the wheel and tire as the height of the tire's side wall may be different from 1 tire to another.
Anyhow if you send say 12 to the function the 12 is the diameter of the wheel not the radius. Just because call it "radius" in the function does not make it a radius. You would need to send (12 / 2) to the function to be a radius.
Thank you for such a thorough answer, I appreciate the effort you put into teaching me what I was doing wrong and explaining how I could fix it. I changed it to the way that you recommended and it is working perfectly, thank you. I'm not sure what you mean about not posting the complete message because that's all that was shown within the error message. And I appreciate that you thought about the radius and diameter dilemma, but this is for a class. It will have no real practical use. I was told that we use 12, 14, and 16 inch radius, but I still commend you on pointing that out. Thank you once again.
An error in the last figure here is about 1 in 1E-18
The diameter of the earth is about 12756200m so the error in circumference would be about 1.27562e-11m which is about 1/100th of a nanometer. IIRC that is the distance light travels in 1/100th of a second - ie not a very long way!
The point is, 3.14 is possibly a bit sloppy but the proposed alternative is not only silly, it definitely has no practical use in this, or in most other calculations anybody is likely to come across.
In MSVS 2015 I knew how to use those defined constants.
Maybe I misunderstood what I read or it did not go far enough, but I had the understanding the in MSVS 2017 and 2019 they were no longer available. Now I see that the "#define" I was using has changed and all those defined constants are in a different file and I was looking in the wrong place.