Writing a program with more than one parameter.

Pages: 12
Jul 23, 2010 at 9:10pm
This entire problem has my mind completely in circles.

Here's the original problem:


I tried writing out the program using the logic I thought would work and here's my code:
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
#include <iostream>
#include <string>

using namespace std;
double displayDivision(double x,double y)
{
	double answer;
	answer = (x / y);
	// if (divisor == 0)
	  // {
	      //cout << "Error: Attempt to divide by zero!" << endl;
	  // }
	// else 
	  // 

}

int main ()
{
	// put this is in a do..while function?
	double dividend, divisor;
	cout << "Enter the dividend: ";
	cin >> dividend;
	cout << "Enter the divisor: ";
	cin >> divisor;
	// call function
	// cout << "Do you want to continue (y/n)?
	// cin >> response;
	// if (response == "n") || (response == "N")
	//		// return 0;

}


My first problem that I run into is that I'm supposed to have my function display the division and not return it. This means that instead of return; it's going to simply be a cout << right?

When I try to run this, I get an error that says my function must return a value.

Any ideas? Maybe my logic is really flawed.
Last edited on Jul 24, 2010 at 12:24am
Jul 23, 2010 at 9:42pm
Line 5 tells the caller to expect a double to be returned. If you put void in there you don't have to return anything.
Jul 23, 2010 at 9:50pm
This means that instead of return; it's going to simply be a cout << right?

Yes

When I try to run this, I get an error that says my function must return a value.

If you don't want a function to return anything use a void function.

Check for zero divisor before doing the calculation.
You will need to use a loop in the main function, which runs until the user wants to continue (I recommend a do-while loop, since you don't wanna ask the user if he/she want's to continue in the first iteration). In the loop you first ask for user input, call your function, then ask the user if he/she want's to continue.

Good luck, come back if you have any further troubles.
Last edited on Jul 23, 2010 at 9:50pm
Jul 23, 2010 at 10:10pm
Here's what I have so far. For some reason, I keep getting that choice is undeclared. I'm clearly defining it, aren't I?

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
34
35
36
37
38
39
40
#include <iostream>
#include <string>

using namespace std;
void displayDivision()
{
	
}

int main ()
{
	do
	{
	double dividend, divisor;
	

	cout << "Enter the dividend: ";
	cin >> dividend;
	cout << "Enter the divisor: ";
	cin >> divisor;

	if (divisor == 0)
	{
		cout << "Error: Attempt to divide by zero!" << endl;
	}
	else 
	{
		double response = (dividend / divisor);
		displayDivision();
	}
    char choice;
	cout << "Do you want to continue (y/n)? ";
	cin >> choice;
	} while ((choice == 'y') || (choice == 'Y'));
		
	
	
	

}
Jul 23, 2010 at 10:17pm
The condition of your while loop can't see choice declared inside of the loop.
Jul 23, 2010 at 10:20pm
The while condition happens outside the loop:
while ((choice == 'y') || (choice == 'Y'));
So you need to declare choice outside the loop also.
Jul 23, 2010 at 10:20pm
Okay.. I moved it before the loop starts and is letting me run. However, the loop isn't running properly.

I tried entering a y and it just ended.
Last edited on Jul 23, 2010 at 10:26pm
Jul 23, 2010 at 10:47pm
I'm baffled, I don't know why my loop isn't working. I just rewrote it just to make sure I didn't typo and I can't see anything wrong with 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
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

void displayDivision()
{
	cout << "hi" << endl;
}


int main ()
{
	double dividend, divisor, choice;

	do
	{
	cout << "Enter the dividend: ";
	cin >> dividend;

	cout << "Enter the divisor: ";
	cin >> divisor;

	if (divisor == 0)
	{
		cout << "Error: Attempt to divide by zero!" << endl;
	}

	else 
	{
		double response = (dividend / divisor);
		cout << response << endl;
		displayDivision();
	}

    cout << "Do you want to continue (y/n)? ";
	cin >> choice;
	
	} while ((choice == 'y') || (choice == 'Y'));
		
	
	
	

}
Jul 23, 2010 at 10:56pm
double dividend, divisor, choice;

Isn't choice meant to be a char?
Jul 23, 2010 at 10:58pm
Oh wow ~_~ One little thing can throw the whole code off.

I feel dumb. Thanks for the response, lol.

The last thing I have to do is make the function. I'm really unsure of how to do this since I can't reference anything.

I know I have to cout << something but I can't do cout << response

Any ideas?
Jul 23, 2010 at 11:08pm
Jul 23, 2010 at 11:33pm
Hm, didn't get anything from that..

I am trying to make my function,
1
2
3
4
void displayDivision()
{
	cout << "hi" << endl;
}


Output the answer without storing it.

Am I missing something big in the link you posted?
Jul 23, 2010 at 11:38pm


I don't understand how this is supposed to work if the function and the program are two different things >________> urgh, mind bend.
Last edited on Jul 24, 2010 at 12:24am
Jul 23, 2010 at 11:43pm
You were pretty close with your original code for this function. The code you commented out looked good.

Jul 23, 2010 at 11:44pm
\assignment27.cpp(19) : error C4716: 'displayDivision' : must return a value

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>

using namespace std;

double displayDivision(double dend, double visor)
{
	double answer = (dend / visor);
	if (visor == 0)
	{
		cout << "Error: Attempt to divide by zero!" << endl;
	}

	else 
	{
		cout << answer << endl;
		}
		
}


int main ()
{
	double dividend, divisor;
	char choice;

	do
	{
	cout << "Enter the dividend: ";
	cin >> dividend;
	


	cout << "Enter the divisor: ";
	cin >> divisor;

	//call the function
	


	

    cout << "Do you want to continue (y/n)? ";
	cin >> choice;

	} while ((choice == 'y') || (choice == 'Y'));
	
	
	
	

}


The instructions tell me not to call the function. /sigh/
Last edited on Jul 23, 2010 at 11:50pm
Jul 23, 2010 at 11:50pm
Why? Just use an if() statement in your function to see if the divisor is zero.

You do know what the divisor is don't you?
Jul 23, 2010 at 11:51pm
Read up on this:

http://www.cplusplus.com/doc/tutorial/functions/

It might help.
Jul 23, 2010 at 11:55pm
I've read that at least a dozen times as it's been suggested before. I know what a function does and how to reference them in the examples they give. The problem says that I have to print this in the function without returning the information.

And yes, the divisor is what I'm dividing by.
Last edited on Jul 23, 2010 at 11:56pm
Jul 24, 2010 at 12:09am
Then you have all the pieces. Like I said, your original code that you commented out was fairly close.
Jul 24, 2010 at 12:09am
xcrossmyheartx wrote:
The instructions tell me not to call the function.

Where do they tell that?

xcrossmyheartx's compiler said:
\assignment27.cpp(19) : error C4716: 'displayDivision' : must return a value

As R0mai said, use a void function. A void function is a function that doesn't return a value. It doesn't mean that it can't take any arguments. That's how your function's signature should look like:

void displayDivision(double dividend, double divisor)
Last edited on Jul 24, 2010 at 12:10am
Pages: 12