hi. can anyone please help me with this assignment?
Write a function called divideMe which takes two integer parameters
x and y and returns a double which is the division of these numbers
(x / y). If y is zero, print the message "Divisor can not be zero!"
and return 9999999.999.

EXAMPLES:

Please enter the dividend: 99
Please enter the divisor: 44
99 divided by 44 is 2.25
Press any key to continue . . .

Please enter the dividend: 22
Please enter the divisor: 7
22 divided by 7 is 3.14286
Press any key to continue . . .

Please enter the dividend: 9
Please enter the divisor: 0
Divisor can not be zero!
Press any key to continue . . .

so far i have the coding, but it is not able to output a double rather than an integer.

#include <iostream>
#include <string>

using namespace std;

double divideMe(int x, int y)
{
	double quotient = x/y;
	return quotient;
}

int main()
{
	int dividend, divisor;
	double quotient;
	cout << "Please enter the dividend: ";
	cin >> dividend;
	cout << "Please enter the divisor: ";
	cin >> divisor;

	quotient = divideMe(dividend,divisor);
	cout << dividend << " divided by ";
	cout << divisor << " is " << quotient << endl;
}

if anyone could give me any suggestions i'd appreciate it. thanks!! :)

In your function, test y for being zero, display the error message if it is.

What kind of results do you get from this code? Are you getting the values you expect?

What type of result do you get when you divide and int by an int? If that's not what you want, what simple action will fix it?

okay thanks!
i edited it, but still it does not return 9999.999 when the divisor is 0.

#include <iostream>
#include <string>

using namespace std;

double divideMe(double x, double y)
{
	if (y == 0)
	{
		cout << "Divisor can not be zero!" << endl;
		return (9999999.999);
	}
	else
	{
		double quotient = x/y;
		return quotient;
	}
}

int main()
{
	int dividend, divisor;
	double quotient;
	cout << "Please enter the dividend: ";
	cin >> dividend;
	cout << "Please enter the divisor: ";
	cin >> divisor;
	quotient = divideMe(dividend,divisor);
	if (divisor != 0)
	{
		cout << dividend << " divided by ";
		cout << divisor << " is " << quotient << endl;
	}	
}

okay i think i got it. i just added a cout statement for 999999.9999 in the divideMe function.

does this look right?
thanks!

Yes it does. But you don't display that returned value anywhere.

Reread your assignment:

Write a function called divideMe which takes two integer parameters

You've changed your function parameters - that'll cost you points.
Fixing the division problem is something you'll have to do inside the function. How do you get two int's to give a double result? That's a key part of this assignment.

okay i think i got it. i just added a cout statement for 999999.9999 in the divideMe function.

does this look right?
thanks!

Nope, again, you're changing what the assignment requires. Is it necessary to display the 9999....? Is that what the examples show?

that key part of this assignment to get two integers to result in a double is exactly what i have no idea how to do..

and the example doesnt show the displaying of 9999.999 but in the assignment it states to display that after telling the user they cant divide by zero. is that not what my professor means?

Yes, the function should take two int's and give back a double. Look up "type casting"

If y is zero, print the message "Divisor can not be zero!" and return 9999999.999.

The return of the 999.... is so that your function has something to return when it cannot do the division. Nothing says that value has to be displayed, either by the function or by the caller.

okay. i get what youre saying about returning 999.9999 thanks!

but im still trying to figure out how to use type casting to convert to doubles..

else
	{

		double quotient = static_cast<double>(x/y);
		return quotient;
	}

is that anything like i should be doing??

Have you looked in your text? Checked your class notes? (Oh, I forgot, students don't take notes anymore, or remove the shrinkwrap from the books :D )

Look here at the section titled "Common usage of type casting."

close. (x/y) is still an int result, you are simply turning that into a double. Consider:
5/2 -> 2
cast that to a double and all you get is 2.0, when we really want 2.5

Hint: fix this by taking something away, not by adding anything more to your code.

YAYY!!!!
okay thanks a ton..

double quotient = (static_cast<double>(x) / static_cast<double>(y));

right??
lol

Yes, you got it:icon_exclaim:

It could be simpler as just

double quotient = static_cast<double>(x) /y;

When x is explicitly cast to double, y will be implicitly cast as well, because the division operator only acts upon two operands of the same type. Implicit casting is always in the upward direction - values get promoted to the higher precision or greater range type.

okay thanks a ton for helping me think through this problem!!! you dont know how badly i need these grades..

:) thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.