![]() |
|
#1
|
|||
|
|||
Perfect Numbers with functionsHey guys, I'm having problem with a typical C++ beginning program where I find all the perfect numbers between 1 and 10000. I have made a program to find this before, but we have been asked to rewrite it using the two following functions:
int sum_of_positive_divisors(int number) and bool is_perfect(int number). If you could help me figure out what I did wrong it would be greatly appreciated. When I compile it there is an error at the bool function, but I feel like there might be more than that. Thanks in advance! CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Perfect Numbers with functionsYou have some semicolons and brackets out of place. Try starting with this as a skeleton:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Perfect Numbers with functionsOk, I didn't realize that the int sum_of_positive_integers and bool weren't included in int main(). I'm now getting an error at:
if (is_perfect(number)) return true; cout << number << " is perfect.\n"; else return false; which mentions an expected primary expression before the else statement, expected ; before the else statement. CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
Re: Perfect Numbers with functionsOK, I updated it a bit. I still have a problem with my else statement, but my main problem is that the cout statement is supposed to be in int main(), but I'm not sure how to do that. If i take out the if else statement completely then there are no errors, but there is obviously no output since I didn't use cout. So basically, what do I put under the bool expression, and how do I use cout in main? I've used it before, but never while referencing a previous function. Hope that made sense.
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Perfect Numbers with functionsThere is something that you are not quite getting yet...brackets. For each opening bracket {, you need a closing bracket } somewhere after it.
When you are using if/else statements, it is primarily best to use brackets like: CPP / C++ / C Code:
CPP / C++ / C Code:
The following will produce compile errors: CPP / C++ / C Code:
Check your code again and see if any of this applies. |
|
#6
|
|||
|
|||
Re: Perfect Numbers with functionsOk, thanks. I see what you're saying.
CPP / C++ / C Code:
The program compiles and runs successfully, but there is still no output since I'm not sure how to include it in int main(). |
|
#7
|
|||
|
|||
Re: Perfect Numbers with functionsQuote:
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: Perfect Numbers with functionsThere are no errors again, but there is no output. When I run the program, it quits responding.
CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: Perfect Numbers with functionsFirst of all, it would only print if the number will be found to be perfect so you may want to print something if the number was not found to be perfect and, as may not have noticed, you never set the number so it will be the value of whatever is at that location in memory (~randomish).
|
|
#10
|
|||
|
|||
Re: Perfect Numbers with functionsAh ok, I didn't realize about the value of the number. I'm not showing output of the non perfect numbers since my range is from 1-10000. That would be a lot of output. I did this:
CPP / C++ / C Code:
but again with no success. Thanks again for your help fake. This is all new to me and you're definitely helping. I appreciate it. |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perfect numbers within a range | aedude94 | C++ Forum | 4 | 09-Oct-2008 08:28 |
| Perfect Numbers | Corrupt Shadow | C++ Forum | 9 | 31-Mar-2008 06:43 |
| Prime or perfect numbers and output factors | buomque | C++ Forum | 4 | 26-Oct-2007 09:23 |
| Linear Search | eccoflame | C Programming Language | 3 | 19-Apr-2005 08:36 |
| Program that will find the first 4 perfect numbers????? | bicknell83 | C Programming Language | 3 | 30-Mar-2005 01:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The