3 Replies - 410 Views - Last Post: 05 August 2014 - 06:14 AM Rate Topic: -----

#1 laxio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 14

classes

Posted 02 August 2014 - 08:20 AM

hi i am a first year student at kumasi polytechnic in Ghana.
wrote this code to find the factorial of any given function ., works fine but when i put it in a class... it gives me an error ::assignment of read only variable fact;
#include <iostream>
using namespace std;
const static int fact=1;
 
class My_Factorial {
	public:
		
		int x;
		void Get_Number(){
	cout<<"enter a number to find its factorial\n";
	cin>> x;
	}
	void Find_Factorial(){
			if(x<=2){
	
	cout<<"the factorial of "<< x <<" is:" <<endl;
	cout<< x <<endl;}
else{

	for(int c=1;c <= x ;c++);
	
	fact = fact*c;
cout<<"the factorial of "<< x <<" is:" << fact <<endl;	
}
}
};
int main(){
My_Factorial obj;
 obj.Get_Number();
 obj.Find_Factorial();
[code]
	system ("pause");
	return 0;
}


This post has been edited by modi123_1: 02 August 2014 - 08:43 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: classes

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,410
  • Joined: 05-May 12

Re: classes

Posted 02 August 2014 - 08:49 AM

Notice that on line 3, you declare fact to be const. Since you declared it constant, the compiler is catching you cheating and trying to change it.

You really don't need an global variables for this type of program.
Was This Post Helpful? 1
  • +
  • -

#3 laxio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 14

Re: classes

Posted 05 August 2014 - 02:39 AM

View PostSkydiver, on 02 August 2014 - 08:49 AM, said:

Notice that on line 3, you declare fact to be const. Since you declared it constant, the compiler is catching you cheating and trying to change it.

You really don't need an global variables for this type of program.

how then should i declare it??
am quite confused
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,410
  • Joined: 05-May 12

Re: classes

Posted 05 August 2014 - 06:14 AM

What have you tried? Did you try taking out the const as a first attempt?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1