Hey guys, I hope I'm not being a pain, but when I get stuck trying to understand something, it helps to come here since you guys seem to knock some sense into me haha! The program below is just a simple area calculator. The purpose of writing this is to learn how to program using Value and Reference parameters using functions. Now of course I'm choosing which ever one is easier, but I can't seem to understand how to link the arguments with the parameters. Could anyone clarify how to do this? Is there an example you could give me? Any help would always be appreciated. Thanks.
PS: Another question comes to mind. Instead of typing "using namespace std;" at the top, I see a lot of people who code it into the program. For example:
std:: cout << "Some text" << endl; I'm pretty sure I'm missing something just before the std part, but I was wondering if I need to get out of the habit of writing "using namespace std;" at the top of my programs to indicate that the whole program will be using the standard region.
// Function Prototype
void building_area(int length, int width, float& acres);
cout << "The area is: " << area << endl;
cout << "\n";
cout << "The area in acres is: " << acres << endl;
int length, width;
cout << "Please enter the Length of the building: ";
cin >> length;
cout << "Please enter the Width of the building: ";
cin >> width;
void building_area(int length, int width, float& acres)
int area = length * width;
float acres = area / 43560.0;
On your first question: You have building_area() defined as taking two integers and a float reference. But when you call the building_area() function in userInput(), you are only passing the two integers.
Also, area is local in scope to the building_area() function.
You then define a local copy of acres in building_area(), over-riding the argument passed in.
Also, in main, you are calling building_area() without ANY arguments!
The fastest way to fix this is to define acres in the userInput() function;
Correct the way you are calling building_area() by passing in acres;
Put your messages in the building_area function;
Take out the call to building_area() in main.
On your second question: When you type using namespace std, you are telling the compiler to reference the functions that are contained in that namespace. That way, you can leave off the scope-resolution operator and save your fingers from having to type so much.
Great! To be able to output the results in your main routine, I suggest that you:
make your variables local to the main routine;
Change userInput() to either:
return the calculated area;
or (probably better) add area to the parameter list to building_area (as a reference)