Originally Posted by
Trekkie
Thank you so much! I can finally follow the code. I didn't know how or where to input the void GetData part. I'm still learning, so thanks for your patience!
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void GetData( float& var1, float& var2 )
{
//PUT THE CODE FOR THE FUNCTION HERE
}
int main()
{
float miles;
float hours;
float milesPerHour;
cout << "Input miles and hours "<<endl;
cin >> milesPerHour;
cout << fixed << showpoint;
cout << "The miles per hour are: " <<endl;
GetData(miles, hours);
//Fill in the code to invoke the function GetData
milesPerHour = miles / hours;
cout << setw(10) << miles
<< setw(10) << hours
<< setw(10) << milesPerHour << endl;
return 0;
}
Looking at your code, it's apparent that you want to read in two(2) values from the user: 'miles' and 'hours'. If the user entered milesPerHour, what would be the purpose of your program? Your program is supposed to calculate the milesPerHour for the user.
You seem to think that a computer will do what you say in a string, e.g.: "Input miles and hours". It doesnt work that way. If it did you could write your whole program like this:
Code:
int main()
{
cout<<"Get two pieces of data from the user, calculate mph, and display it."
return 0;
}
A computer doesn't understand the English language. All that statement will do is display that exact string in the console window, and then do nothing. For your program to work, you have to write the C++ statements that will read in two values, miles and hours, in the correct order, and then because mph is defined to be miles divided by hours, you have to do the division and display the result.
I would suggest you buy a beginning C++ book, like "C++: A Beginner's Guide". In the meantime, here is some information on functions:
-----
Here is a very simple function:
Code:
#include <iostream>
using namespace std;
void showNumber()
{
cout<<100<<endl;
}
int main ()
{
showNumber();
return 0;
}
Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:
showNumber();
That says, "please go find a function named showNumber, and execute the lines of code contained therein."
Here is another example:
Code:
#include <iostream>
using namespace std;
void showNumber(int a)
{
cout<<a<<endl;
}
int main ()
{
showNumber(100);
showNumber(25);
return 0;
}
Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:
showNumber(100);
To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:
cout<<a<<endl;
Here is another example:
Code:
#include <iostream>
using namespace std;
int addTen(int a)
{
int answer = a + 10;
return answer;
}
int main ()
{
int result1 = addTen(2);
int result2 = addTen(5);
cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
return 0;
}
This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.
To summarize:
1) A function must list a return type before its name.
2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
3) When you call a function, the value you send it must match the function parameter.
4) The function call in main() is replaced by the function return value.
5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
Code:
void showNumber(void)
{
cout<<100<<endl;
}
(void) is equivalent to ( )