0

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

6
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by MosaicFuneral
0

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??

0

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

0

>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.

0

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!

0

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".

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.