Hello dasdas,
No that is not what
Furry Guy meant.
Looking at your code:
First line 14 tries to print an uninitialized variable. This should have produced an error. Aside from being uninitialized where does "product" get a proper value? The line is better placed after the while loop.
The next problem comes in the while loop. You are trying to use a variable with a garbage value, this could be (-858993460), and may be different on your computer. So consider what is -858993460 * 1 * 1 * 1 * 1 * 1? Or -858993460 * 2 * 2 * 2 * 2 * 2? This is not what you are wanting and along the way it will end up being (0).
Not only should you initialize the variable it should start with the value of 1.
temp = temp;
You do realize that this means and does nothing. Not sure what you are expecting setting a variable = to its-self.
You will have to explain what
temp * temp * temp * temp * temp
is for because I do not understand the point.
Another problem I found after getting the program to run is with the variable "product". You have defined this as an "int", but do you realize what the value of "product" could become in the while loop.
When I first got the program to run it produced this output:
Enter the number to determine the product of the recedent numbers: 10
The products of the receding numbers of 1 2 3 4 5 6 7 8 9 10
10 are : 17,875,765,589,461,434,368
|
There is a good chance that your computer may define the limits of an "int" as:
Size of a int is: 4 bytes. With a range of: -2,147,483,648 to 2,147,483,647.
Size of a unsigned int is: 4 bytes. With a range of: 0 to 4,294,967,295.
|
Do you see the problem with the while loop?
I reworked your code a bit and came up with this:
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
|
#include<iostream>
#include <locale> // <--- Optional. Not needed.
using namespace std;
int main()
{
int number{}, temp = 1;
unsigned long long product{ 1 };
std::cout.imbue(std::locale("")); // <--- Requires header file <locale>. Optional. Not needed.
cout << "Enter the number to determine the product of the recedent numbers: ";
cin >> number;
cout << "\nThe products of the receding numbers of ";
while (temp <= number)
{
cout << temp << " ";
if (number > temp)
{
product *= temp * temp * temp * temp * temp;
}
//temp = temp; // <--- Has no affect.
temp++;
}
cout << "\n\n" << number << " are : " << product << '\n'; // <--- Moved to here.
return 0; // <--- Not required, but makes a good break point.
}
|
In line 13 it says "recedent", but the while loop is going in the opposite direction. It is a minor thing, but something to consider.
Some other minor points:
int main() {
. Although it makes no difference to the compiler this is the way I most often see it written.
In your code you have used 2 styles of {}. Pick 1 style and be consistent in its use. It really helps readability.
If, for, while and do/while loops do not need to define a block for just 1 line, but a good habit would be to use the {}s even for just 1 line in case you have to add more late. This way you will not have to remember to add them later. Once you get use to it you do not really think about it.
Andy