Please don't "help" by introducing more advanced concepts.
The problem is the way you are using the function.
cout << displayDivision() << endl;
This code says you are writing something to the standard output, no matter what or how that something is obtained. The command is not optional, and the kind of thing you are writing is always going to be the same thing (in this function's case, a
double, not a
string).
cout << "Enter first number: ";
The next problem is you are mixing an input operation into an output operation. This is a bad idea.
Bazzy's suggestion methinks is best:
Why don't you output the result in the function? |
Part of the problem is the name of the function, "
displayDivision()". It is misnamed because it:
1 - doesn't display the division (rather, it
returns the result of the division)
2 - it does more than display something -- it asks for input (the numbers to divide)
A better name might be something as silly as
void getAndDisplayUserDivision()
. This function gets two numbers from the user, divides them, and displays the result.
On a side note, you always break the loop so there is no need to have it.
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
|
#include <everything>
void getAndDisplayUserDivision()
{
double x, y;
cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;
if (y!=0)
cout << x << " divided by " << y << " is " << x/y << endl;
else
cout << "Error!";
}
int main()
{
char answer;
do
{
getAndDisplayUserDivision();
cout << "Do you want to continue (y/n)" << endl;
cin >> answer;
}
while (answer!='n');
return 0;
}
|
As always, take care to use proper indentation and spacing to make your programs easier to read.
Hope this helps.