我被分配了一个项目,我无法解决问题。该项目是: “main()函数,它要求用户输入以计算以下内容:SumProductDifference和Power。应该有一个设计良好的用户界面。一个名为SumProductDifference的void函数(int,int,int&,int&,int& amp; ;)计算机两个输入参数的和,乘积和差值,并通过引用传递和,乘积和差值。一个值返回函数叫做Power(int a,int b),计算一个上升到b的幂。使用迭代控制结构甚至递归来设计和实现自己的幂函数。不要简单地在名为pow()的C ++函数周围编写包装器。应该有一个用户循环和一个菜单,以便用户可以选择SumProductDifference, Power或Quit。菜单还应提供允许用户设置和更改两个输入整数值的选项。
#include "stdafx.h"
#include <iostream>
using namespace std;
void SumProductDifference(int, int, int&, int&, int&);
int Power(int a, int b);
bool GoAgain();
int main() {
int a, b;
int choice;
int sum, product, difference;
do
{
cout << "================================================" << endl;
cout << "=====Welcome to AB Calculator 2016 Edition======" << endl;
cout << "==== 1 - SumProductDifference Function ======" << endl;
cout << "==== 2 - Power of Function ======" << endl;
cout << "==== 3 - Quit ======" << endl;
cout << "= Make A Selection: ======" << endl;
cout << "================================================" << endl;
cin >> choice;
switch (choice) {
case '1': SumProductDifference(a, b, sum, product, difference);
break;
case '2': Power(a, b);
break;
case '3':
break;
default:
break;
}
return 0;
} while (choice != 3);
return 0;
}
bool GoAgain() {
char answer;
cout << "would you like to go again (y/n) ==> ";
cin >> answer;
return answer == 'y';
}
void SumProductDifference(int a, int b, int& s, int& p, int& d) {
cout << "Enter two integers: ";
cin >> a, b;
s = a + b;
p = a * b;
d = a - b;
cout << "The sum of " << a << " + " << b << " = " << s << endl;
cout << "The product of " << a << " * " << b << " = " << p << endl;
cout << "The difference of " << a << " - " << b << " = " << d << endl;
}
int Power(int a, int b) {
int total = 1;
int i;
cout << "Enter a number: ";
cin >> a;
cout << "Raise this integer to the power of: ";
cin >> b;
for ( i = 1; i < b; i++) {
total = total *b;
}
return total;
}
答案 0 :(得分:-1)
在转换中声明int choice;
- case '1'
?
您正在阅读int
,但将其与char字面值进行比较。
更改int choice;
- &gt; char choice;
或case '1'
- &gt; case 1:
阅读C / C ++中的基本类型。 Char只是一个8位整数,我们可以方便地选择代表一个字符(在编译器的同意下)。
此外,您无法正确读取std :: cin:cin >> a, b;
中的多个值以及其他一些错误。
学会正确研究。在询问之前请咨询cppreference.com。