c++相似的重复输入具有不同的输出

  • 本文关键字:输出 相似 c++ c++
  • 更新时间 :
  • 英文 :

#include<iostream>
#include "cmath"
using namespace std;
double power(double a1, double b2)
{
double result;
result = pow(a1,b2);
return result;
}
int main()
{
int a1, b2, result;
int choice = 0;
int count = 4;
int r1,r2,r3,r4;
while (choice < count)
{
cout <<"Enter the value of coefficient." << endl
<< "Coefficient: ";
cin >> a1;
cout <<"Enter the value of the exponent." << endl << "Exponent: ";
cin >> b2;
choice++;
}
if (a1 == 0 && b2 == 0)
{
cout << "You entered 0 values. " << endl;
}
else
{
r1 = power(a1,b2);
r2 = power(a1,b2);
r3 = power(a1,b2);
r4 = power(a1,b2);

cout << "The answers are: " << endl
<< r1 << endl << r2 << endl << r3 << endl << r4 << endl;
}
return 0;
}

我需要显示不同的输出值,但它只给了我完成的最后一次输入计算。那么,我怎么可能显示第一个/第二个值呢?这里是初学者,所以我愿意接受一些批评。

我不确定你想做什么,但我假设你想问8个数字(4次a和4次b(。并且您想要显示4个结果(r1, r2…(。但是,当您第二次请求ab时,您只需在执行cin >> a1;时用新值覆盖它们

对于您正在做的工作,我鼓励您了解阵列

因此,您可能应该在覆盖它们之前进行计算,并将结果存储在数组:中

// declare the array of size 4
int resultArray[4];
...
...
cin >> b2;
r1 = power(a1,b2);
resultArray[choice] = r1
choice++;
}
cout << "First result :" << resultArray[0]

或者你可以暂时存储你的价值观,并在后进行数学运算

// declare 2 arrays of size 4
int coefficientArray[4];
int exponentArray[4];
...
cin >> b2;
coefficientArray[choice] = a1;
exponentArray[choice] = b2;
choice++;
}
...
r1 = power(coefficientArray[0],exponentArray[0]);
r2 = power(coefficientArray[1],exponentArray[1]);

最新更新