DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Wierd Numbers appear when trying to run a running product in a C++ program (http://www.daniweb.com/forums/thread22473.html)

MrNiceGuy82 Apr 24th, 2005 8:29 pm
Wierd Numbers appear when trying to run a running product in a C++ program
 
Im having trouble with the Running product in my C++ program. Where is the question:
2. Write a program that repeatedly reads in integer values until a value less than or equal to zero is entered. For each value, it should print the product of the numbers from 1 to the given value. An ending message should be printed. Use a for statement to help compute the running product. A sample run might be (user input in bold):

Enter an integer: 3

The product of numbers from 1 to 3 is 6

Enter an integer: 9

The product of numbers from 1 to 9 is 362880

Enter an integer: 12

The product of numbers from 1 to 12 is 479001600

Enter an integer: 5

The product of numbers from 1 to 5 is 120

Enter an integer: -3

Thank Goodness!

Now here is the part in the program where i believe im having a little trouble. I'll copy and paste below where i believe my problem is occuring:

In my function definition i have this:

void print_product ( int user_number, int product)
{

int count;

product = 1;


cout << "\nThe product of numbers from 1 to " << user_number << " is " << product;

for (count = 1; count <= user_number; count++)
{


product = count * product;

cout << product;

}


And as for a example when i run this program like this ill get a answer like:

Enter an integer: 5

The product of numbers from 1 to 5 is 112624120 <---- which is incorrect.

So if anyone could help me to fix this problem i would really appreciate it.

Thank You

Narue Apr 24th, 2005 8:32 pm
Re: Wierd Numbers appear when trying to run a running product in a C++ program
 
>cout << product;
Replace that with this:
cout << product << '\n';

MrNiceGuy82 Apr 24th, 2005 9:08 pm
Re: Wierd Numbers appear when trying to run a running product in a C++ program
 
Thanks so much for your time and help. Now i would like to ask one more question if thats ok. Now for the product i want the product of numbers through 1 through a number entered by the user to equal the total product for example: 1*2*3*4*5 = 120.

For some reason im getting the product if i enter something similar like the example above this:

Enter an integer: 5

The product of numbers from 1 to 5 is 1
2
6
24
120

Now how would i get rid of those extra multiples? The only number i need to show in my program is the very last number like this:

Enter an integer: 5

The product of numbers from 1 to 5 is 120

If you have any solutions feel free to write back. If not thank you again anyways :)

Narue Apr 24th, 2005 9:13 pm
Re: Wierd Numbers appear when trying to run a running product in a C++ program
 
Move the print outside of your loop so that you don't print intermediate values:
for (count = 1; count <= user_number; count++)
{
  product = count * product;
}

cout << product <<endl;


All times are GMT -4. The time now is 2:21 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC