2014-04-05 52 views
0

我用C++編寫了這個簡單的計算器。它不斷地接受輸入,但是遇到了麻煩,把我的操作變量定義爲char,因爲它讓我無法退出程序。這裏的代碼:簡單的C++連續計算器,不能退出程序

#include <iostream> 

int main(){ 
    long double num1, num2; char operation; 
    std::cout << "Welcome to the calculator!\n\nInput numbers and operations to begin. (2+2) Then hit enter.\n\nThe calculator will continue to expect an operation and number (e.g. +6)\nuntil you enter \"q\" as an operation.\n\nEnter \"q\" as an operation to quit.\n\n"; 
    std::cin >> num1 >> operation >> num2; 
    do{ 
     switch(operation){ 
      case '+': 
       num2 += num1; 
       break; 
      case '-': 
       num2 -= num1; 
       break; 
      case '/': 
       num2 /= num1; 
       break; 
      case '*': 
       num2 *= num1; 
       break; 
      default: 
       std::cout << "Not a valid operation. Try again."; 
       break; 
     } 
     std::cout << num2; 
    } while (std::cin >> operation >> num1); 
    return 0; 
} 

該程序運行良好,完美的作品,我只是不知道如何退出。我試圖讓'q'返回0,但它似乎不工作,因爲我的do-while需要2個輸入...任何想法或想法?

回答

1
std::cin >> num1 >> operation >> num2; 
do 
{ 
switch(operation){ 
case '+': num2 += num1; break; 
case '-': num2 -= num1; break; 
case '/': num2 /= num1; break; 
case '*': num2 *= num1; break; 
default: std::cout << "Not a valid operation. Try again."; break; } 
std::cout << num2; 
std::cin >> operation; 
if (operation == 'q') break; 
else std::cin >> num1; 
} 
while (1); 

樣品I/O 3 + 4 7 + 3 10Q 端 3 + 4

+0

好的,現在我有一個新問題。如果我輸入2-9,它會吐出7,而不是-7。但是如果我輸入2 + -9,它會吐出-7。爲什麼這樣做? – UnderTheSi

+0

如果我輸入2-9,它會在我的控制檯中吐出-7。你修改了代碼嗎? – Emu

+0

不,我有相同的代碼。有趣的是,如果我通過了一個正數的第一次運行,那麼在此之後所有時間都會顯示負數。沒關係,我明白了。正如@DieterLücking在下面提到的那樣,我的' - '和'/'操作搞砸了。謝謝你們的幫助! – UnderTheSi

0

嘗試做這樣的。

std::cin >> num1 >> operation >> num2;  

do{ 
    std::cout << "Enter operation"; 
    std::cin >>operation; 
    switch(operation){ 
     case '+': 
      num2 += num1; 
      break; 
     case '-': 
      num2 -= num1; 
      break; 
     case '/': 
      num2 /= num1; 
      break; 
     case '*': 
      num2 *= num1; 
      break; 
     case 'q': 
      return 0;  //or exit(0); 
     default: 
      std::cout << "Not a valid operation. Try again."; 
      break; 
    } 
    std::cout << num2; 
} while (true); 
+0

這種方法不允許我將「-100」加到我現有的號碼上,那麼用戶就必須再次輸入num1,一個操作和num2。 – UnderTheSi

+0

好的,如果你想對相同的數字進行操作,那麼現在看看解決方案。 – Himanshu

+0

不會只是永遠循環打印2 + 2,4,6,8等? – UnderTheSi

0

這是我的要求。

int main() 
{ 
    long double num1, num2; 
    char operation; 
    bool stop = false; 
    std::cout << "Welcome to the calculator!\n\nInput numbers and operations to begin. (2+2) Then hit enter.\n\nThe calculator will continue to expect an operation and number (e.g. +6)\nuntil you enter \"q\" as an operation.\n\nEnter \"q\" as an operation to quit.\n\n"; 

    std::cin >> num1 >> operation >> num2; 

    do{ 
     switch(operation) { 

      case '+': 
       num2 += num1; 
       break; 

      case '-': 
       num2 -= num1; 
       break; 

      case '/': 
       num2 /= num1; 
       break; 

      case '*': 
       num2 *= num1; 
       break; 

      default: 
       std::cout << "Not a valid operation. Try again."; 
       break; 
     } 
     std::cout << num2 << std::endl; 

     // Skip till the end of the line. 
     while (std::cin.get() != '\n'); 

     // Read the next operation. 
     std::cin >> operation; 
     if (operation == 'q') 
     { 
      stop = true; 
     } 
     else 
     { 
      std::cin >> num1; 
     } 

    } while (std::cin.good() && !std::cin.eof() && !stop); 
    return 0; 
} 
0
case 'q': exit(0); 

case 'q': std::exit; 

不要忘記添加cstdio頭文件。