以下是我的作业链接:http://prnt.sc/e9q619
如果满足,交换机中的默认值将变为无限循环,而且我也无法从分配中执行其他重要注释。当我去另一个导师时,他试图告诉我使用我们尚未在课堂上学习的方式使用向量和字符串,所以如果使用的'c'和'x'没有固定就没关系。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int i = 1;
int Num1 = 1;
int Num2 = 1;
char operation;
int answer = 1;
cout << "Here is a four function integer calculator." << endl;
cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl;
cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl;
while (i > 0)
{
cin >> Num1 >> operation >> Num2;
switch (operation)
{
case '+':
answer = Num1 + Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '-':
answer = Num1 - Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '*':
answer = Num1 * Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '/':
if (Num2 == 0 && operation != 'x') {
break;
}
else {
answer = Num1 / Num2;
cout << "The solution is: " << answer << "." << endl;
}
break;
case 'c':
system("cls");
break;
case 'x':
return 0;
break;
default:
cout << "You entered a wrong number, operator, or form for the entire operation" << endl;
break;
}
}
cout << "The solution is: " << answer << "." << endl;
}
答案 0 :(得分:0)
所以我所做的是我单独检查每个输入并编写输入验证语句。
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int i = 1;
int Num1 = 1;
int Num2 = 1;
char operation;
int answer = 1;
cout << "Here is a four function integer calculator." << endl;
cout << "Please use 'x' to exit the calculator, and 'c' to clear the calculator in the operator space. E.G. ""1 x 3"" would exit the calculator." << endl;
cout << "Enter the first number, operation, and second number in respective order, separated by a space each." << endl;
while (i > 0)
{
cout << "Enter Num1: " << endl;
cin >> Num1;
cout << "Enter operation: " << endl;
cin >> operation;
cout << "Enter Num2: " << endl;
cin >> Num2;
while (operation != '+' && operation != '-' && operation != '*' && operation != '/')
{
cout << "You Suck. Try again." << endl;
cout << "Enter Num1: " << endl;
cin >> Num1;
cout << "Enter operation: " << endl;
cin >> operation;
cout << "Enter Num2: " << endl;
cin >> Num2;
}
switch (operation)
{
case '+':
answer = Num1 + Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '-':
answer = Num1 - Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '*':
answer = Num1 * Num2;
cout << "The solution is: " << answer << "." << endl;
break;
case '/':
if (Num2 == 0 && operation != 'x') {
break;
}
else {
answer = Num1 / Num2;
cout << "The solution is: " << answer << "." << endl;
}
break;
case 'c':
system("cls");
break;
case 'x':
return 0;
break;
default:
cout << "You entered a wrong number, operator, or form for the entire operation" << endl;
break;
}
}
cout << "The solution is: " << answer << "." << endl;
}