Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 19th, 2008, 3:29 PM   #1
adenus
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 adenus is on a distinguished road
Question Why my program dont cout perfect number?

#include <iostream>
using namespace std;

//aprekjina funkcija

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
               sum = sum + i;
            }
        }
        if (sum == rob) {
          result=sum;
        }
return (result);
}
//galvena funkcija
int main() {
int rez;
int lim;
    cout<<"ievadi robesu lidz kurai meklet : ";
          cin>>lim ;       
    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;
    system ("pause");
return 0;
}

why dont it cout perfect numbers, ? and in one forum for me advice to inicialise result, but i dont know which value add to it?
adenus is offline   Reply With Quote
Old Mar 19th, 2008, 3:59 PM   #2
adenus
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 adenus is on a distinguished road
Re: Why my program dont cout perfect number?

pls help!
adenus is offline   Reply With Quote
Old Mar 19th, 2008, 4:06 PM   #3
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 87
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Why my program dont cout perfect number?

Quote:
    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;
Rethink the logic here.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Mar 19th, 2008, 4:11 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
Re: Why my program dont cout perfect number?

Don't bump threads. If people have an answer to offer, they will. If you don't get a reply, it means people can't help or are disinclined to.

In any event, the code;
for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
cout<<"perfect numbers from 1 to"<<lim<<" : "<<rez<<endl;
only prints out the value of lim, and the value of rez produced in the last iteration through the for loop. If you want to print out every value that rez takes, put the output statement inside the loop.
grumpy is offline   Reply With Quote
Old Mar 19th, 2008, 4:24 PM   #5
adenus
Newbie
 
Join Date: Mar 2008
Posts: 3
Rep Power: 0 adenus is on a distinguished road
Re: Why my program dont cout perfect number?

#include <iostream>
using namespace std;
//aprekjina funkcija

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
               sum += i;
            }
            if (sum == rob) { 
               return true;
            } else {
               return false;       
            }
        }
return true;
}
//galvena funkcija
int main() {
int rez;
int lim;
    cout<<"ievadi robesu lidz kurai meklet : ";
          cin>>lim ;       
    for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }
        cout<<"perfektie skaitļi no 1 lidz "<<lim<<" : "<<endl<<rez<<endl;
    system ("pause");
return 0;
}
//ievadot jau zinamu sk, to ari izvada, bet citus nee
if i have like that is it right, or what i have to correct, because this advice to move cout into loop, dont help.?
adenus is offline   Reply With Quote
Old Mar 19th, 2008, 4:50 PM   #6
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
Re: Why my program dont cout perfect number?

int Perfectnumber (int rob) {
    int result;
    int sum = 0;
        for (int i = 1; i < rob; i++) {
            if (rob % i == 0) {
               sum += i;
            }
            if (sum == rob) { 
               return true;
            } else {
               return false;       
            }
        }
return true;
}

is wrong for a start

the if sum == rob statement should be outside of the for loop, otherwise your exiting after the first iteration, you need that loop to finish calculating sum before you do this if check.

for (int i = 1; i < lim; i++) {
        rez=Perfectnumber(lim);
    }

I don't understand why you need to check if n is a perfect number n amount of times. Perhaps you should look at your code and understand whats happening.
Seif is offline   Reply With Quote
Old Mar 19th, 2008, 6:41 PM   #7
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
Re: Why my program dont cout perfect number?

Quote:
Originally Posted by adenus View Post
if i have like that is it right, or what i have to correct, because this advice to move cout into loop, dont help.?
The advice helps as much as it is possible, given the way you asked your question.

The basic problem in your code is that you have code outside loops that need to be inside the loops, and other things inside loops that need to be outside. Hence the code runs incorrectly. That is enough information for you to fix your problem, if you just think about it a bit.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guess a Number 3n! C++ 7 Dec 2nd, 2007 3:38 AM
Language display in program Prm753 C++ 3 May 30th, 2006 5:45 PM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 1:15 PM
Support With Perfect Number. TecBrain C++ 1 Mar 7th, 2005 10:17 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC