2016-06-06 89 views
-5

我不知道把所有循環部分放在哪裏放置一個簡單的y/n(重複/退出)循環。我試圖找到答案,但沒有一個是清楚的足以滿足我的特殊情況。 P.S.在的IAM編碼一個初學者,所以請不要弄得太複雜,除非必要 這是我到目前爲止的代碼我在哪裏把do-while循環放在一個計算器程序中

#include <stdio.h> 
#include <iostream> 
using namespace std; 

// input function 
void Input (float &x, float &y); 

float a=1.0, b=1.0, result; 
char operation; 
char yesNO; 

int main() 

{ 
do { 
    cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n"; 

    cout << "Geef een opdracht (eg. 1 + 2): \n"; 
    cin >> a >> operation >> b; 

    Input (a,b); 

    cout << "Het antwoord is: " << result << endl; 
    system ("pause"); 
    return 0; 
} 
while (yesNO == 'y'); 
void Input (float &x, float &y) 
{ 
    a = x; 
    b = y; 

    switch (operation) 
    { 
     case '+': 
      result = x + y; 
      break; 

     case '-': 
      result = x - y; 
      break; 

     case '*': 
      result = x * y; 
      break; 

     case '/': 
      result = x/y; 
      break; 

     default: 
      cout << "foutieve invoer: \n"; 
      cin >> a >> operation >> b; 
      Input (a, b); 
    } 
    } 
} 
+1

[使用std命名空間是壞在這裏看到(http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c -passside-bad-practice) –

+0

'while'循環在正確的位置,但你不想在循環內部使用'return',並且你希望在循環內部設置'yesNO'。 – user4581301

+0

看起來像你搞砸了你的示波器。你不能在main()中聲明'Input()'。 –

回答

-2

由於「int main」中的return語句而停止。我會建議使用「void main()」而不是「int main()」。但是如果你想使用「int main()」,把while語句下面的「return 0」移回去。您還需要詢問用戶是否要繼續。試試這個:(忽略壞的間距)

int main() { 
    do { 
     cout << "Programma wat optelt, aftrekt, vermedigvuldigd en deelt. \n\n"; 

     cout << "Geef een opdracht (eg. 1 + 2): \n"; 
     cin >> a >> operation >> b; 

     Input (a,b); 

     cout << "Het antwoord is: " << result << endl; 
     cout << "Press y to continue: "; 
     cin >> yesNo; 
    } while (yesNO == 'y'); 
    return 0; 
} 
+0

請你能理清縮進 –

+1

什麼是'yesNO'實際上?另外'「按y繼續:」'是誤導用戶的。你必須輸入'y'和ENTER。按'y'鍵是不夠的。 –

+0

建議使用'void main()'?!?!這不是C++。可以在一些編譯器上工作,但嚴格來說不允許 – user463035818

-1

我會忽略一些東西不對您的程序,直接回答這個問題。

兩件事情:

  1. 你永遠不會詢問用戶是否要繼續
  2. 你被returing出的主()中止你的循環

所以更換這些2號線:

system ("pause"); 
return 0; 

使用查詢詢問用戶是否要繼續並填充變量yesNO與他們的答案。