Whenever I run this program, i get the error "expected initializer before 'int'" on the line of "int primefunc", how can i fix this?
#include <cstdlib>
#include <iostream>
using namespace std;
int Input;
int Divisor;
int Prime
int primefunc(int Input)
{
for (Divisor= 1;Divisor<=Input;Divisor++) //Calculates Factors of Input
{
if(Input%Divisor==0)
{
for (Prime= 1;Prime<=Divisor;Prime++)
{
if((Divisor%Prime)>0) //Finds prime factors
{
cout<<Divisor<<"are the prime factors of "<<Input<<endl;
}
else
{
return 0;
}
}
}
else
{
}
}
}
int main()
{
cout<< "Enter a Number:\n";
cin>>Input;
if (x%1==0)
{
int primefunc(Input)
}
else
{
cout<< "Please enter a positive integer."<<endl;
crap...im an idiot. So i fixed that and it runs the program but does not display any thing other than "enter a number' and then its just blank
#include <cstdlib>
#include <iostream>
using namespace std;
int Input;
int Divisor;
int Prime;
int primefunc(int Input)
{
for (Divisor= 1;Divisor<=Input;Divisor++) //Calculates Factors of Input
{
if(Input%Divisor==0)
{
for (Prime= 1;Prime<=Divisor;Prime++)
{
if((Divisor%Prime)>0) //Finds prime factors
{
cout<<Divisor<<"are the prime factors of "<<Input<<endl;
}
else
{
return 0;
}
}
}
else
{
}
}
}
int main()
{
cout<< "Enter a Number:\n";
cin>>Input;
if (Input%1==0)//Checks for a postive integer
{
int primefunc(Input);
}
else
{
cout<< "Please enter a positive integer."<<endl;
When writing code its best if you write an allgorithm, then take it piece by piece and make sure it works.
For example, you're trying to check for positive numbers but you're modulating by 1.
Do you know what you're doing when you do that?
1 2 3 4 5 6 7 8 9
if (Input%1==0)//Checks for a postive integer
{
int primefunc(Input);
}
else
{
cout<< "Please enter a positive integer."<<endl;
}
if not wouldnt the easier solution for a positive integer be
1 2 3 4 5 6 7 8 9
if (Input > 0)//Checks for a postive integer
{
int primefunc(Input);
}
else
{
cout<< "Please enter a positive integer."<<endl;
}
Understand it before you code it, then test it to make sure all parts work as intended, then move forward.
You also should prompt for re-input and stick it all in loop. Else it will say "Please enter positive integer" then exit the program, or just stay running since you dont return 0 at the end of your code (depends on what youre using to compile program)