help with C++ program

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 23
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

help with C++ program

 
0
  #1
Nov 12th, 2008
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
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int main() {
  5.  
  6. int n;
  7. bool devisor;
  8. cout << "enter number";
  9. cin >> n;
  10.  
  11. for (int i = 0 ; i < n/2 ; i++ ){
  12. devisor = true;
  13. if ( n % i = 0 ){
  14. devisor = false;
  15. }
  16. }
  17. if(devisor){
  18. cout << i << "is a devisor" << end1;
  19. }
  20. return 0;
  21. }
any extra help will be much appreciated
Last edited by Narue; Nov 12th, 2008 at 12:58 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 36
Reputation: freudian_slip is an unknown quantity at this point 
Solved Threads: 4
freudian_slip's Avatar
freudian_slip freudian_slip is offline Offline
Light Poster

Re: help with C++ program

 
0
  #2
Nov 12th, 2008
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??
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 23
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Re: help with C++ program

 
0
  #3
Nov 12th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with C++ program

 
1
  #4
Nov 12th, 2008
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,275
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with C++ program

 
0
  #5
Nov 12th, 2008
Taking the mod of 0 might be a problem as well, since you declare i to start at zero.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 23
Reputation: nedsnurb is an unknown quantity at this point 
Solved Threads: 0
nedsnurb nedsnurb is offline Offline
Newbie Poster

Re: help with C++ program

 
0
  #6
Nov 12th, 2008
thanks a lot guys, now it compiles correctly however wont display the divisors??

code now looks like this
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int main() {
  5.  
  6. int i;
  7. int n;
  8. bool devisor;
  9. cout << "enter number" <<endl;
  10. cin >> n;
  11.  
  12. for (int i = 1 ; i < n/2 ; i++ ){
  13. devisor = true;
  14. if ( n % i == 0 ){
  15. devisor = false;
  16. }
  17. }
  18. if(devisor){
  19. cout << i << "is a devisor" << endl;
  20. }
  21. return 0;
  22. }
i know im in the realms of retardation to you guys but help is a godsend!
Last edited by Narue; Nov 12th, 2008 at 3:21 pm. Reason: added code tags, please do it yourself next time
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 16
Reputation: AcidG3rm5 has a little shameless behaviour in the past 
Solved Threads: 0
AcidG3rm5 AcidG3rm5 is offline Offline
Newbie Poster

Re: help with C++ program

 
0
  #7
Nov 12th, 2008
can you try this...

  1. if(devisor==true)
  2. {
  3. cout << i << "is a devisor" << endl;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 980
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: help with C++ program

 
0
  #8
Nov 12th, 2008
Originally Posted by AcidG3rm5 View Post
can you try this...
  1. if(devisor==true)
  2. {
  3. cout << i << "is a devisor" << endl;
  4. }
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".
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 441 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC