| | |
help with C++ program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 23
Reputation:
Solved Threads: 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
any extra help will be much appreciated
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)
#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; }
Last edited by Narue; Nov 12th, 2008 at 12:58 pm. Reason: added code tags
>error C2106: '=' : left operand must be l-value
In your code you say
>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.
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
•
•
Join Date: Nov 2008
Posts: 23
Reputation:
Solved Threads: 0
thanks a lot guys, now it compiles correctly however wont display the divisors??
code now looks like this
i know im in the realms of retardation to you guys but help is a godsend!
code now looks like this
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Narue; Nov 12th, 2008 at 3:21 pm. Reason: added code tags, please do it yourself next time
•
•
Join Date: Oct 2008
Posts: 16
Reputation:
Solved Threads: 0
can you try this...
C++ Syntax (Toggle Plain Text)
if(devisor==true) { cout << i << "is a devisor" << endl; }
•
•
•
•
can you try this...
C++ Syntax (Toggle Plain Text)
if(devisor==true) { cout << i << "is a devisor" << endl; }
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: heap creation c++
- Next Thread: Order of operations!
Views: 441 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct studio temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






