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.
#include <iostream>
usingnamespace 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;
}
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.
#include <iostream>
usingnamespace 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;
}