hi guys
am a total noob with C++ and im trying to write a program that will take a user inputted integer and displays its divisors and then tell the user whether it is a perfect number or not.

So far I have

#include <iostream>

using namespace std;
int main() {

int n;
bool devisor;
cout << "enter number";
cin >> n; 

for (int i = 0 ; i < n/2 ; i++ ){
	devisor = true;
	if ( n % i = 0 ){
	devisor = false;
	}
}	
if(devisor){
	cout << i << "is a devisor" << end1;
}
return 0;
}

any extra help will be much appreciated

Recommended Answers

All 7 Replies

What is it that you are having problem with? Are you getting errors when you compile? Does it compile but doesn't display the correct values??

well it doesn't compile it throws out the following errors:

error C2106: '=' : left operand must be l-value
error C2065: 'i' : undeclared identifier
error C2065: 'end1' : undeclared identifier

>error C2106: '=' : left operand must be l-value
In your code you say if ( n % i = 0 ){ . C++ uses == for equality and = for assignment, so you're using the wrong operator.

>error C2065: 'i' : undeclared identifier
This is a simple one: you haven't declared the variable i anywhere, so you can't use it.

>error C2065: 'end1' : undeclared identifier
This is also simple, and probably due to poor choice of fonts in your text editor. It's endl with a lower case L, not the number 1. Think of endl as standing for "end line" and you shouldn't have this problem anymore.

Member Avatar for iamthwee

Taking the mod of 0 might be a problem as well, since you declare i to start at zero.

thanks a lot guys, now it compiles correctly however wont display the divisors??

code now looks like this

#include <iostream>

using namespace std;
int main() {

int i;
int n;
bool devisor;
cout << "enter number" <<endl;
cin >> n; 

for (int i = 1 ; i < n/2 ; i++ ){
	devisor = true;
	if ( n % i == 0 ){
	devisor = false;
	}
}	
if(devisor){
	cout << i << "is a devisor" << endl;
}
return 0;
}

i know im in the realms of retardation to you guys but help is a godsend!

can you try this...

if(devisor==true)
{
cout << i << "is a devisor" << endl;
}

can you try this...

if(devisor==true)
{
cout << i << "is a devisor" << endl;
}

Yes, it's the same thing. Since if() checks if the statement is true.
When you say if(condition == false) you're asking, "if it is true that this condition is equivalent to false".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.