I have a problem with my factorial loop

Hey, so I have a problem with my factorial loop here:

// Factorial.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int k=1;
int n;
int factorial=1;

cout<<"Define n";
cin >> n;

while (k<=n)
{
cin >>k;
factorial=factorial*k;
k=k+1;
}
cout << "n!="<<factorial;
}

Like the program runs, but I enter n, and then, I have to enter the k values myself and the program doesn't enter them for me.

How do I fix this??

Thanks!
Noy
k=k+1;
You can just put "K++".
So every time the loop executes, you'd like 'k' to increase by 1? The problem here is that you have still left in cin >> k;. This is used to get the user's input, so that's why you have to enter the values!

Remove that line, and you should be good to go. (If you feel like cleaning up your code, change 'k = k+1' to 'k++' and maybe switch the while loop into a for loop.
Thanks a lot. I fixed the code and it now works well. :)
Topic archived. No new replies allowed.