Factorials Classes

Hey uh so I have a program to create and i don't understand the problems with this code.
One of them is "expected ';' before 'for'" and i don't understand since I'm new to this. Also inside the for loop it says statement has no effect. Please help

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

class Factorials{
private:
int value;
int compute;

public:
void setValue (int theValue){
theValue = value;
}

void setCompute (int theCompute){
theCompute = compute;
};

int getValue () {
return value;
}

int getCompute () {
return for(int i = 1; i <= value; i++) {
value * (value - i);
}
};
};

int main () {
Factorials calculate;
int value = 0;
int compute = 0;

cout << "Enter a positive Interger: ";
cin >> value;

cout << compute;

calculate.setValue(value);

cout << "Factorial of " << value << " = " << calculate.getCompute () << endl;

return 0;
}
Hello Brandon17,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

The offending function:
1
2
3
4
5
6
7
int getCompute()
{
	return for (int i = 1; i <= value; i++)
	{
		value * (value - i);
	}
};  // <--- Do not need the ; 

The comment tells one problem. The bigger problem is that the for loop does not return a value that the "return" statement can return.

Something that might work for you is: total = value * (value - i); and then return "total".

1
2
3
4
5
6
7
8
9
10
11
12
int getCompute()
{
	int total;

	for (int i = 1; i <= value; i++)
	{
		total = value * (value - i);
	}

	return total;
};  // <--- Do not need the ; 


Hope that helps,

Andy
Sorry it's my first time.
Well that helped my program run but it doesn't give me the correct answer.
When i enter a positive integer I always get the number 7208676 no matter what number I enter, and I'm trying to get the factorials
1
2
3
4
5
6
7
void setValue (int theValue){
theValue = value;
}

void setCompute (int theCompute){
theCompute = compute;
};


problem is here

1
2
3
4
5
6
7
void setValue (int theValue){
value = theValue;
}

void setCompute (int theCompute){
compute = theCompute;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

class Factorials {
private:
	int value;
	int compute;

public:
	void setValue(int theValue) {
		value = theValue;
	}

	void setCompute(int theCompute) {
		compute = theCompute;
	}

	int getValue() {
		return value;
	}

	unsigned long long getCompute() {
		unsigned long long total = value;
		for (int i = 1; i < value; i++) {
			total *= (value - i);
		}
		return total;
	}
};

int main() {
	Factorials calculate;
	int value = 0;
	int compute = 0;

	cout << "Enter a positive Interger: ";
	cin >> value;

	calculate.setValue(value);

	cout << "Factorial of " << value << " = " << calculate.getCompute() << endl;

	cin.ignore();
	cin.get();
	return 0;
}


Revised with Andy's tips.
Last edited on
Hello Brandon17,

As saeidsj has pointed out your setter functions are backwards.

Look at what you have:
1
2
3
4
void setValue (int theValue)
{
    theValue = value;
}

What this is doing is setting "theValue" equal to "value" which has no value at this point.

Think about the logic here and the way this works. "theValue" receives what was sent to the function with the intention of changing the private variable of the class variable. In the case of assignment (=) this is done right to left. Be careful in the future because this is not the only thing that works right to left.

Now for the function "getCompute()" the for loop is all wrong. What I showed you earlier is more of a concept not the answer.

Try going to the top of the page and in the search box enter "factorial" after the first links that will take you off site look at the forth link, next to last post at the bottom of the post the function will give you a good idea of what to do for your function.

One last point. consider what is the largest number an "int" or "unsigned int" can hold? I am not sure at what number the factorial will produce a number larger than an "int" is, but I know that 20 do not work. It is worth your time to figure what each type of variable can hold. I even wrote a program once to display this information so I would not have to look it up every time.

Hint: look st what an "unsigned long long" can hold.

Hope that helps,

Andy
Okay Thank you for your help everyone I got it to work but can someone explain to me what this line does?
1
2
3
4
5
6
unsigned long long getCompute() {
		unsigned long long total = value;
		for (int i = 1; i < value; i++) {
			total *= (value - i);
		}
		return total;

I understand the rest of the code but haven't studied about unsigned as yet or long long. My last request, i promis
Hello Brandon17,

As you will come to learn there are many types of variables. You may know about bool,char, int and float and double. In between a char and int there is short.

The difference between a signed variable and unsigned variable is that a signed variable can store both negative and positive values whereas an unsigned variable can only positive values.

This is about the best table I have found describing the variable types and what they can hold.
https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx

Looking at the table you can see that some of the variable types the unsigned version can hold a positive value twice the size of the signed version, but not for all. This is a good page to bookmark and print out.

I did not test the program, but somewhere between 10 and 20 the result of the for loop becomes larger than an int can hold.

Not long ago there was a topic here where the coder tried to tore a ten digit phone number in an int and I think he/she was wondering why it did not work.

Hope that helps,

Andy
Yes thanks you I get I it
You have been real helpful and i understand a bit more now
Topic archived. No new replies allowed.