# classes

Page 1 of 1

## 3 Replies - 567 Views - Last Post: 05 August 2014 - 06:14 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=351464&amp;s=aa839f6ab854cbe4b4a657376c530741&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 laxio

Reputation: 0
• 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

• Code herder

Reputation: 4376
• Posts: 14,124
• 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.

### #3 laxio

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

## Re: classes

Posted 05 August 2014 - 02:39 AM

Skydiver, 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

### #4 Skydiver

• Code herder

Reputation: 4376
• Posts: 14,124
• 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?

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }