Program Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shoothy
    New Member
    • May 2010
    • 1

    Program Help

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int subP(int num){
    	int ans[1000];
    	for(int x=0;x<=num;x++){
    		double test = 0;
    		test = num/x;
    			if(test == int(num/x)){
    				ans[x]=x;
    			}
    	}
    
    	cout<<endl;
    	cout<<num<<endl;
    
    	for(int y=0;y<=num;y++){
    		if(ans[y]=0){
    			cout<<y<<" ";
    		}
    	}
    	int sum=0;
    	for(int z=0;z<num;z++){
    		sum=sum+z;
    	}
    	return sum;
    }
    
    int main(){
    Start:
    	int num=0;
    	int sum=0;
    	cout<<"Type a number: ";
    	cin>>num;
    	if(num==0){
    		goto end;
    	}
    	sum = subP(num);
    	cout<<endl<<sum;
    	if(sum<num)
    		cout<<endl<<"Deficient";
    	if(sum==num)
    		cout<<endl<<"Perfect";
    	if(sum>num)
    		cout<<endl<<"Abundant";
    	cout<<endl<<endl;
    	goto Start;
    end:
    	cout<<"the end";
    }
    I Have no idea why. But the program builds fine, yet i get errors during the running
    Last edited by Banfa; May 11 '10, 11:15 PM. Reason: Added [code] ... [/code] tags
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    What error are you getting while running? Could you please post the error message?

    --edit, you're dividing by 0. Change your for loop to start from 1.
    I'm surprised you don't get a division by zero error. Or maybe you do.

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I can not see the purpose of the for loop at line 19?

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        That may be written for debugging purpose.

        Regards
        Dheeraj Joshi

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Also in the if clause you have
          if(ans[y]=0){
          which is an assignment operation You want:
          if(ans[y]==0){
          The comparative operation.

          Comment

          Working...