void


if i remove void from a function will it still work?
It depends where the "void" is. If you are talking about the return type of the function you have to leave it there, or replace it with a type name if you want the function to return a value.
// prime or perfect
#include <iostream>
using namespace std;
void isPrime(int);
void isPerfect(int);

int main( )
{
unsigned int n;
cout << "enter a number to see isPrime or isPerfect: ";
cin >> n;
isPrime(n);
isPerfect(n);

return 0;
}

void isPrime(int n)
{
cout << "\nin isPrime function: ";
bool b=true;
for(int i=2; i<n; i++)
if(n%i==0)
b=false;

if(b) cout << "Prime.\n";
else cout << "not Prime.\n";
}

void isPerfect(int n)
{
cout << "\nin isPerfect function: ";
int sum=0;
for(int i=1; i<n; i++)
if(n%i==0)
sum+= i;

if(sum==n)
cout << "Perfect.\n";
else cout << "not Perfect.\n";
}
thats the example someone told me
this is wat i already had
and my question was
Write your question here.
i cant seem to understand how to place number to plug in on their own and be able to use a number being plugged in(Output a list of all divisors of numbers that are not prime and a list of divisors of all perfect numbers. Format your output so there are between 5 and 10 numbers per line .) if you can just explain that would be great really having trouble.
the thing is im a beginner and i really cant use void because i have not learned it and i have to get numbers that are not prime and perfect numbers to appear.


#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//get their number
int main()
{
{
int perfectnumber;
int number;
char response, prime;

cout << "Not sure if its a prime number or perfect number." << endl;
cout << "Would you like to find out if your number is prime or perfect?" << endl;
cin >> response;

cout << "

if (response == 'Y' || response == 'y')
{
cout << "Please enter any number you would like to check" << endl;
cin >> number;
}
else if (response == 'n' || response == 'N')
{
cout << "thanks come try me again soon!" << endl;
system("pause");
return 0;
}
else if (response != 'y' || response != 'Y' || response != 'N' || response != 'n' )
{
cout << "wrong input try again" << endl;
system ("pause");
}
//prime
if ((number / 1 == number) && (number / number == 1))
{
cout << "It is a prime number!" << endl;
}
else if ((number % 2 == 0 || number % 3 == 0 || number % 4 == 0 || number % 5 == 0 || number % 6 == 0 || number % 7 == 0 || number % 8 == 0 || number % 9 == 0))
{
cout << "Not a prime :(" << endl;
}
//perfect number
{
int i=1,sum=0;
while(i<number){
if(number%i==0)
sum=sum+i;
i++;
}
if(sum==number)
cout << i << " is a perfect number!";
else
cout << i << " is not a perfect number :(";
system("pause");
return 0;
}
}
}
Last edited on
You need to include void in a declaration & definition, but not when you call that function.
Topic archived. No new replies allowed.