[英]C++ Factor Program: Outputting Number of Factors Given
I am writing a simple program that finds the factors of a list of integers through Linux Redirection.我正在编写一个简单的程序,通过 Linux 重定向查找整数列表的因子。 I am almost done, but I am stuck on one part.
我快完成了,但我被困在了一个部分。 Here is my program so far:
到目前为止,这是我的程序:
#include<iostream>
using namespace std;
int main()
{
int counter = 0;
int factor;
cin >> factor;
while (cin)
{
if (factor < 0)
break;
cout << "The factors of " << factor << " are " << endl;
for(int i=factor; i>=1; i--)
if (factor % i == 0)
{
counter++;
cout << i << endl;
}
cout << "There are " << " factors." << endl;
cout << endl;
cin >> factor;
}
return 0;
}
Now the problem I have is in the line " cout << "There are " << " factors." << endl; ".现在我遇到的问题是“cout <<”有“<<”因素。“<<endl;”。 I'm not sure how to calculate the number of factors output by the program.
我不确定如何计算程序输出的因子数。
For example:例如:
The factors of 7 are 7的因数是
1 1
7 7
There are 2 factors.有2个因素。
How would I go about calculating and outputting the "2" in this example.在这个例子中,我将如何计算和输出“2”。
Help is greatly appreciated.非常感谢帮助。
Instead of代替
cout << "There are " << " factors." << endl;
use用
cout << "There are " << counter << " factors." << endl;
If you do that, you have to move the line where you define counter
.如果这样做,则必须移动定义
counter
的行。
Instead of it being the first line in main
, it needs to be moved to be the first line in the while
loop.它不是
main
中的第一行,而是需要移动到while
循环中的第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.