While loop to a For loop

Hello everyone how would I convert this while loop program to a for loop program?



#include <iostream>
using namespace std;

int main()
{
int x = 0; // Start with 0 so the numbers entered can go up to 5
int Total = 1; //
int userinput;


while (x != 5) // This code is for a while loop
{
cout << "input a number ";
cin >> userinput;
x++;
Total *= userinput;

}
cout << "Total of the 5 numbers entered is :" << Total << endl;

system("pause");
return 0;
}
Hello abedaoud1,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Change while (x != 5) to for (int x = 0; x < 5; x++) and remove the "x++" from inside the {}s.

You may also want to change the type for "Total". One very large number and "Total" could exceed the range of what an int can store. You might consider a "long long" for the type of "Total".

Hope that helps,

Andy
Hey Andy,

Thank you so much for the help I appreciate it.

Abe
Topic archived. No new replies allowed.