我的实验室教授希望我们制作一个程序,使用for循环编写一个程序来计算用户输入的x整数的乘积;其中x也是由用户输入的。使用while或do-while重复问题。"
在进一步澄清之后,我意识到他的意思就像用户输入一个数字,比如说5.然后程序将提示用户输入5个数字。然后程序显示这5个输入数字的乘积。我现在知道它是如何工作的,但是我不知道如何存储所有这些数字并将它们带出来进行乘法运算。
到目前为止,这就是我的全部内容:
#include <iostream>
using namespace std;
int main() {
int numofNumbers = 0;
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
for (numofNumbers; numofNumbers > 0; numofNumbers = numofNumbers - 1) {
cout << "Enter a number"; //how can I record these values then multiply them?
cout << endl;
}
system("pause");
return 0;
}
答案 0 :(得分:0)
您假设您需要存储所有用户提供的数字以便将它们相乘 - 这不是真的。我建议花更多的时间思考如何解决你的作业而不存储所有传递的数字。
为了回答您的问题:在C ++中存储项目的最常用方法是使用std::vector
。为了理解它在做什么,你应该对arrays有一些了解,并仔细阅读文档。
答案 1 :(得分:0)
您不需要存储号码。只需输入一个数字即可。
#include <iostream>
using namespace std;
int main() {
unsigned int numofNumbers = 0;
int product = 0;
int number = 0;
int tempNumofNumbers = 0;
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
tempNumofNumbers = numofNumbers;
while(tempNumofNumbers) {
cout << "Enter a number";
cin >> number;
if(tempNumofNumbers == numofNumbers)
{
product = number;
}
else
{
product *= number;
}
tempNumofNumbers--;
}
cout << "product" << product;
system("pause");
return 0;
}
答案 2 :(得分:0)
如果不需要存储N编号,则可以在地点上乘以值:
int numofNumbers = 0;
int result = 1
cout << "Enter the number of numbers you want multipled: ";
cin >> numofNumbers;
for (; numofNumbers > 0; numofNumbers = numofNumbers - 1) {
cout << "Enter a number";
int num = 1;
cin >> num;
result *= num;
}
if (numofNumbers > 0) {
cout << "Multiplication result is: " << result;
}
答案 3 :(得分:0)
所以你设法计算出这些数字的因子,现在你想存储它们以便以后使用它。
您可以使用STL中的矢量容器来存储您的号码。贝娄就是一个例子:
/usr/lib/hive/lib/hive-jdbc.jar
/usr/lib/hive/lib/libthrift-0.9.2.jar
/usr/lib/hive/lib/hive-service.jar
/usr/lib/hive/lib/httpclient-4.2.5.jar
/usr/lib/hive/lib/httpcore-4.2.5.jar
/usr/lib/hive/lib/hive-jdbc-standalone.jar
/usr/lib/hadoop/client/hadoop-common.jar
答案 4 :(得分:0)
对于录制输入,您可以使用:
#include <iostream>
int main() {
int numofNumbers = 0;
std::cout << "Enter the number of numbers you want multipled: ";
std::cin >> numofNumbers;
double total = 1;
for (int counter = numofNumbers; counter > 0; counter--)
std::cout << "Enter a number: " << std::endl;
std::cin >> input_number;
total = total * input_number; //multiple the numbers
}
if (numofNumbers > 0) {
std::cout << "total is: " << total << std::endl;
}
system("pause");
return 0;
}
答案 5 :(得分:0)
#include <iostream>
using namespace std;
int main()
{
int n, count = 0;
int input, result = 1;
cout<<"Enter number: ";
cin>>n; // n numbers
while (count < n)
{
cout<<"Enter number "<<count + 1<<": ";
cin>>input; // taking input
result *= input; // multiplying
count++;
}
cout<<"Total result is: "<<result;
return 0;
}