我想使它所以如果一個字符輸入在開始,而不是1,2,3或4那麼它將循環回輪,除了說ç簡單的計算器錯誤
cout << "You have entered a incorrect operator" << endl;
我已經嘗試了包括default:
的情況,但似乎並沒有影響它的數量。
任何人都可以擺脫任何光線?
#include <iostream>
#include <string>
void start()
{
using namespace std;
cout << "Welcome to my Basic Mini-Calculator!" << endl;
}
int choice()
{
using namespace std;
int uChoice;
cout << endl << "What do you want to do?" << endl;
cout << "1) Add" << endl;
cout << "2) Subtract" << endl;
cout << "3) Multiply" << endl;
cout << "4) Divide" << endl;
cout << endl << "Waiting for input... (enter a number): ";
cin >> uChoice;
cout << endl;
while(uChoice != 1 && uChoice != 2 && uChoice != 3 && uChoice != 4);
switch (uChoice)
{
case 1:
cout << endl << "You chose addition." << endl;
break;
case 2:
cout << endl << "You chose subtraction." << endl;
break;
case 3:
cout << endl << "You chose multiplication." << endl;
break;
case 4:
cout << endl << "You chose division." << endl;
break;
}
return uChoice;
}
int input(bool i = false)
{
using namespace std;
string text;
text = (i == true) ? "Enter another number: " : "Enter a number: ";
cout << endl << text;
float number;
cin >> number;
return number;
}
int work(int one, int two, int todo)
{
using namespace std;
float answer;
switch (todo)
{
case 1:
answer = one + two;
break;
case 2:
answer = one - two;
break;
case 3:
answer = one * two;
break;
case 4:
answer = one/two;
break;
default: cout << "Please choose a proper number (1-4)" << endl;
}
return answer;
}
void answer(int theanswer)
{
using namespace std;
cout << endl << "The answer is " << theanswer << "." << endl;
cout << endl << "Hit Return to exit.";
cin.clear();
cin.ignore(255, '\n');
cin.get();
}
int main()
{
using namespace std;
start();
int todo = choice();
float one = input();
float two = input(true);
float theanswer = work(one, two, todo);
answer(theanswer);
return 0;
}
將代碼放入問題_(在創建_short_示例程序之後,因爲129行太多)。 SO是一個問答檔案,你的問題沒有問題。 – 2013-02-10 11:51:25