problem with while loop

I have a program that make an array of the input number's receding number, then multiply them using the while loop. But why is it that the output always 0? Is there something wrong with the code?

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
[code]#include<iostream>

using namespace std;

int
main() {
    int number, temp = 1, product, num;

    cout << "Enter the number to determine the product of the recedent numbers : " << endl;

    cin >> number;

    cout << "The products of the receding numbers of ";
    cout << number << " are : " << product << endl;

    while (temp <= number)
    {
        cout << temp << " ";
        if (number >= temp)
            product *= temp * temp * temp * temp * temp;
        temp = temp;
        temp++;
    }

    cout << endl;

    return 0;
}
[/code]
Last edited on
You are displaying the supposed product before you actually calculate it.

PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

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

HINT: you can edit your post and add code tags.
so, I'll be needing to swap their places like:
if (number >= temp)
product *= temp * temp * temp * temp * temp;
temp = temp;
temp++;
cout << temp << " ";?
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
input number's receding number, then multiply them


Does this mean proceeding numbers? Where does temp ^ 5 (temp to power 5) come into this?

The product of a numbers positive proceeding numbers is the factorial of the number (n!). Is this is what is wanted? eg 6! is 6 * 5 * 4 * 3 * 2 * 1 = 720 ??

Now that you've used code tags commenting on your source code is easier.

1. Lines 13-14 need to be placed AFTER you calculate your product in the loop in lines 16-23. Your program is essentially this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   int number;
   int product;

   std::cout << "Enter the number to determine the product of the recedent numbers :\n";

   std:: cin >> number;

   std::cout << "The products of the receding numbers of ";
   std::cout << number << " are : " << product << '\n';
}


2. You declare your variable 'product' yet you assign no initial value to it on creation. Therefore whatever value is contained in it when you calculate the product is used.

Your compiler seems to be "assisting" you by initializing variable values to zero on creation when you don't initialize them with with a value. That is not good.

Variables should never be used without giving them an initial value, even if they are used for input. Always initializing your variables with a value on creation will save you from a lot of headaches later.

With Visual Studio 2019 your code won't compile because of uninitialized variables.

With Code::Blocks 2003 the code compiles with just a warning, the variable 'product' displays it contains a value of 0.

3. How you calculate the product doesn't make sense at all. Possibly like 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
#include <iostream>

int main()
{
   std::cout << "Enter the number to determine the product of the recedent numbers: ";
   int number { };
   std::cin >> number;
   std::cout << '\n';

   int product { 1 };
   int temp    { number };

   while (temp > 1)
   {
      product *= temp;

      // test output to show temporary results in the loop
      std::cout << "   ( " << product << " )\n";

      temp--;
   }
   std::cout << '\n';

   std::cout << "The products of the receding numbers of ";
   std::cout << number << " are : " << product << '\n';
}

Enter the number to determine the product of the recedent numbers: 10

   ( 10 )
   ( 90 )
   ( 720 )
   ( 5040 )
   ( 30240 )
   ( 151200 )
   ( 604800 )
   ( 1814400 )
   ( 3628800 )

The products of the receding numbers of 10 are : 3628800


4. Using an int to hold your products will work for calculations of small numbers, like 10. Input 13 (or larger) and you get integer overflow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Enter the number to determine the product of the recedent numbers: 13

   ( 13 )
   ( 156 )
   ( 1716 )
   ( 17160 )
   ( 154440 )
   ( 1235520 )
   ( 8648640 )
   ( 51891840 )
   ( 259459200 )
   ( 1037836800 )
   ( -1181456896 )
   ( 1932053504 )

The products of the receding numbers of 13 are : 1932053504

That's bad.

Using an unsigned int only prolongs the problem temporarily. Use an unsigned long long and you can have larger starting numbers.

Even an unsigned long long will eventually suffer integer overflow with big enough numbers.

I have a program that make an array

5. You are not using an array (int arr[10];) anywhere in your code.

C++ doesn't allow "variable length arrays." You can't get a number from the user and then create an array with the input as the size*.

C++ has a variable length container, std::vector.
http://www.cplusplus.com/reference/vector/vector/

*Well, C++ does support variable length arrays, but they have to be created using pointers and allocating memory via new. I doubt you've learned any of that yet.
Last edited on
Topic archived. No new replies allowed.