0

So it's 5:00 A.M. where I am and I am confused and frustrated as hell. I've created a program like this before but can't understand what is going on.

I've created a very simple factorial program and have double checked the logic, but every time I enter a number greater than 2 the program goes into a loop constantly printing out "inf". I can't see ANYTHING wrong with the program itself. :(

#include <iostream>

using namespace std;

int main() {
    double userNumber = 0;
    double i = 1;

    cout << "This program will calculate the factorial of the number you enter.\nPlease enter your number now: ";
    cin >> userNumber;

    for ( i = 1; i < userNumber; i++ ) {
        userNumber *= i;
        cout << userNumber;
}

    cout << "\n\nThe factorial is " << userNumber << "." << endl;

    return 0;
}

It works for 1 and 2:

enter image description here

But as soon as you do 3 or greater....

enter image description here

I haven't created a C++ program in a while but can't for the life of me see what is wrong. Is this just a super obvious syntax error, or is my computer finally breaking down on me?

EDIT: I just changed the numbers from double to int and this is what I got:

enter image description here

I still don't understand why it is doing this. I don't see a problem with the for loop, or anything else...

Argh. Smashes head into desk

Qwurticus
  • 877
  • 1
  • 10
  • 18
  • The output you show doesn't match the program in the question. ("The factorial of is ..." versus "The factorial is ..."). Please always make sure that the output you show is from the actual program code in the question. Also, please don't have screenshots of *text*. Instead copy-paste the text into the question and format it properly (use e.g. `
    ` tags)
    – Some programmer dude Apr 14 '14 at 10:25
  • 1
    Never modify a loop control variable in the loop body. – Daniel Daranas Apr 14 '14 at 10:33

3 Answers3

4

Change

for ( i = 1; i < userNumber; i++ ) {

to

for ( i = userNumber - 1; i > 1; --i ) {

Note that your code changes userNumber in the loop body, so that the loop will only terminate if userNumber overflows.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • That fixed it. Thanks. I still don't get why my loop wasn't working, but I'll figure that out after some sleep. – Qwurticus Apr 14 '14 at 10:28
  • @Qwurticus you're increasing `userNumber` inside the loop. So `i < userNumber` is always `true`. – Henrik Apr 14 '14 at 10:31
  • @Qwurticus - the key thing to remember is that the "while" part of a `for` loop (the `i < userNumber` here) is evaluated _every time around the loop_, not once at the beginning to set the number of loops. – TripeHound Apr 14 '14 at 10:45
  • I know, it was a stupid mistake on my part. Probably has to do with the fact that I started it at 5 A.M., lol. :) – Qwurticus Apr 14 '14 at 11:34
0
for ( i = 1; i < userNumber; i++ ) {
        userNumber *= i;
        cout << userNumber;
}

this is the problem : let userNumber = 5
i=1 , userNumber=5
i=2 , userNumber=10
i=3 , userNumber=50 ......

and loop goes on forever . because everytime i<userNumber.

Also as general advice, don't compare doubles , use (a-b) < epsilon; where elipson is required precision .

Aseem Goyal
  • 2,683
  • 3
  • 31
  • 48
0

The problem is that you're using one variable both as a loop bound and as a running accumulator - userNumber increases every time through the loop so you will never reach it.
(The expression i < userNumber doesn't use the value userNumber had when you started looping, but always compares to the current value after each round through the loop.)

Use a separate variable for the result:

int factorial = 1;
for ( i = 1; i < userNumber; i++ ) {
    factorial *= i;
molbdnilo
  • 64,751
  • 3
  • 43
  • 82