943,714 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 579
  • C++ RSS
Nov 12th, 2008
0

help with C++ program

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 12th, 2008
0

Re: help with C++ program

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??
Reputation Points: 11
Solved Threads: 4
Light Poster
freudian_slip is offline Offline
36 posts
since Oct 2008
Nov 12th, 2008
0

Re: help with C++ program

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
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 12th, 2008
1

Re: help with C++ program

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2008
0

Re: help with C++ program

Taking the mod of 0 might be a problem as well, since you declare i to start at zero.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 12th, 2008
0

Re: help with C++ program

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

code now looks like this
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
nedsnurb is offline Offline
27 posts
since Nov 2008
Nov 12th, 2008
0

Re: help with C++ program

can you try this...

C++ Syntax (Toggle Plain Text)
  1. if(devisor==true)
  2. {
  3. cout << i << "is a devisor" << endl;
  4. }
Reputation Points: 6
Solved Threads: 0
Light Poster
AcidG3rm5 is offline Offline
25 posts
since Oct 2008
Nov 12th, 2008
0

Re: help with C++ program

Click to Expand / Collapse  Quote originally posted by AcidG3rm5 ...
can you try this...
C++ Syntax (Toggle Plain Text)
  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".
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: heap creation c++
Next Thread in C++ Forum Timeline: Order of operations!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC