Return Value for calling a function

Nov 8, 2014 at 9:31am
I have to write a program that calls a function. Almost everything runs fine, except for the return part of the function. Up to this point in my class we just used return 0. I tried a few different methods, the code below is just one of those ways. I looked online and they talked about using void, but we have never discussed it in class.

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
  #include <iostream>
using namespace std;
int isMultiple(int number)
{
	char ans;
	if (number % 7 != 0)
		cout << number << " is not a multiple of 7!";
	else
		cout << number << " is a multiple of 7!";
	ans = '_';
	
	return ans;


	
}
int main()
{
	char ans3;
	int number;
	do
	{
		cout << "Enter the number you would like to test.\n";
		cin >> number;
		cout << isMultiple(number);
		cout << "\nDo you want to input another number?  ";
		cin >> ans3;
		cout << endl;
		

	} while (ans3 !='n');
	
	
	return 0;
}
Nov 8, 2014 at 10:23am
Your function int isMultiple(int number) has a return type of int.

However, in your function you are trying to return a char (char ans).

So if you change the definition isMultiple to char isMultiple(int number) then at least your code will compile. Then you can work out whether you really need to cout << isMultiple(number);, or whether just calling isMultiple(number); is sufficient.
Nov 8, 2014 at 2:06pm
try changing the function into char function since you are returning char
Nov 8, 2014 at 7:10pm
Okay thanks for the tips. I tried to do a char version before, but I gave up early because I thought I might be on the wrong track.
Nov 8, 2014 at 7:48pm
Well here is my working code. Thanks for the help guys.
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
#include <iostream>
using namespace std;
char isMultiple(int number)
{
	char ans;
	if (number % 7 != 0)
		cout << number << " is not a multiple of 7!";
	else
		cout << number << " is a multiple of 7!";
	ans = ' ';
	
	return ans;


	
}
int main()
{
	char ans3;
	int number;
	do
	{
		cout << "Enter the number you would like to test.\n";
		cin >> number;
		cout << isMultiple(number);
		cout << "\nDo you want to input another number?  ";
		cin >> ans3;
		cout << endl;
		

	} while (ans3 !='n');
	
	
	return 0;
}
Topic archived. No new replies allowed.