Thread: lvale problem in function definition/

  1. #1
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37

    lvale problem in function definition/

    Code:
    #include<iostream>
    #include<conio.h>
    
    void perfect(int);  // function prototype
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        int num;    // declare varibale
        
        cout<< "Enter an integer:\t";   // prompt the user for input
        cin>> num;  // read num
        
        perfect(num);
    
    getch();
    }
    
    // definition perfect()
    
    void perfect(int userChoice)
    {
        int sum,rem;
        
        for(int value=1;value<=userChoice;value++)
        
        rem=userChoice/value;
        sum+=value;
        int result=1+sum;
        
        if((userChoice/value)==0 && result=userChoice)
            cout<< "perfect";
        else
            cout<< "not perfect";
    }
    4 erors in this program. It is supposed to tell wether a number is perfect number or not.
    int sum,rem;

    for(int value=1;value<=userChoice;value++)

    rem=userChoice/value;
    sum+=value;
    int result=1+sum;

    if((userChoice/value)==0 && result=userChoice)
    cout<< "perfect";
    else
    cout<< "not perfect";
    The user inputs 'userChoice'. userChoice is divided by the 'value'. After that it sums up all the value and if result is equal to the num it couts' perfect, yet some logical errors in the program.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
     if((userChoice/value)==0 && result=userChoice)
    I think u meant...
    Code:
     if((userChoice/value)==0 && result==userChoice)
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    okeys it reduces to 3 errors if i do so but the compiler gives this error now
    :...:' in function void perfect(int).

    &

    :39: name
    lookup of `value' changed for new ISO `for' scoping

    sum+=value;
    compiler highlights the above.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    only other real problem i can see is sum is uninitialised at first use and you ought to return 0 at end of main out of courtesy especially as not all compilers may support the implicit return 0. Oh and sorry this should have been correct line...
    Code:
    if(userChoice/value==0 && result==userChoice)
    Last edited by Stoned_Coder; 01-14-2004 at 07:00 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    Ofcourse '==' is 100% right. I am using a DEV-C++ compiler and it gives 3 errors for the following code;

    Code:
    #include<iostream>
    #include<conio.h>
    
    void perfect(int);  // function prototype
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
        int num;    // declare varibale
        
        cout<< "Enter an integer:\t";   // prompt the user for input
        cin>> num;  // read num
        
        perfect(num);
    
    getch();
    }
    
    // definition perfect()
    
    void perfect(int userChoice)
    {
        int sum,rem;
        
        for(int value=1;value<=userChoice;value++)
        
        rem=userChoice/value;
        sum+=value;
        int result=1+sum;
        
        if((userChoice/value)==0 && result==userChoice){
            cout<< "perfect";}
        else
            cout<< "not perfect";
    }

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Do you need brackets for you for loop?

    or is it only supposed to loop next line?

  7. #7
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    brackets?

    whats the logic behind brackets??

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    your for loop will only loop through the following statement without brackets
    Code:
    for ()
    statement;
    
    so you need brackets
    for ()
    {
     statement1;
     statement2;
    }
    
    this will loop the two statements instead of just the first

  9. #9
    Registered User o0o's Avatar
    Join Date
    Dec 2003
    Posts
    37
    okeys thanku. I'll keep that in mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM