2013-02-10 82 views
-3

我想使它所以如果一个字符输入在开始,而不是1,2,3或4那么它将循环回轮,除了说ç简单的计算器错误

cout << "You have entered a incorrect operator" << endl; 

我已经尝试了包括default:的情况,但似乎并没有影响它的数量。

任何人都可以摆脱任何光线?

http://pastebin.com/9U7sEEzL

#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; 
} 
+0

将代码放入问题_(在创建_short_示例程序之后,因为129行太多)。 SO是一个问答档案,你的问题没有问题。 – 2013-02-10 11:51:25

回答

2

只需添加一个do

do 
{ 
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); 
1

当您尝试读取了号码,但您会收到无效的输入为,流进入一个“失败国家”。您可以检查这个“cin.fail()”,或者你使用整个流作为参数:

// try reading 
while(!(cin >> n)) 
{ 
    cin.clear(); // reset fail state 
    string s; 
    getline(cin, s); // discard remaining line 
} 

这就是说,请降低你的代码下一次小例子。

+0

最小示例...? 我在最后把这个放在我无效的答案中?我认为那是它应该去的地方,但是当我在开始时输入一个字符的时候它并没有消除永远循环的问题。 – user2058678 2013-02-10 18:10:05

+0

查看http://sscce.org/了解最小示例的含义。我不知道你指的是“最后的无效答案”,所以我不清楚你的问题在哪里。你试过运行这段代码吗? – 2013-02-10 19:29:59

+0

试过了。没有工作。 – user2058678 2013-02-10 20:32:14