#include <iostream>
using namespace std;
int main()
{ int a;
int b;
int sum;
string ans = "";
cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
cin >> ans;
if(ans == "Calc")
{
cout << "Welcome to Calculator! Put in a number. Press Enter to put in number. \n";
cin >> a;
cout << "Next number \n";
cin >> b;
sum = a + b;
cout << sum << " Is the amount! \n";
cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
ans = "";
}
if(ans != "Calc")
{
cout << "Okay";
}
}
这样可行,但是如果我不输入Calc,它什么都不做,但打印出来,但是如果我不再输入Calc,它会关闭。如果我输入计算,我可以通过它,当它给出答案,它没关系,然后我按任意键,它关闭。我是这个论坛/网站的新手,不确定是否正确的地方。
答案 0 :(得分:1)
好吧,如果你想让程序再次从用户那里获取输入,那么你应该写它以便它做到这一点:
#include <iostream>
using namespace std;
int main()
{ int a;
int b;
int sum;
while(true)
{
string ans = "";
cout << "Input a directive. Upon it finishing it will terminate. Codes are: \n Calc \n Exit \n";
cin >> ans;
if(ans == "Calc")
{
cout << "Welcome to Calculator! Put in a number. Press Enter to put in number. \n";
cin >> a;
cout << "Next number \n";
cin >> b;
sum = a + b;
cout << sum << " Is the amount! \n";
}
else if(ans == "Exit")
{
cout << "Bye!\n";
return 0;
}
cout << "Okay\n";
}
}