我不知道把所有循環部分放在哪裏放置一個簡單的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);
}
}
}
[使用std命名空間是壞在這裏看到(http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c -passside-bad-practice) –
'while'循環在正確的位置,但你不想在循環內部使用'return',並且你希望在循環內部設置'yesNO'。 – user4581301
看起來像你搞砸了你的示波器。你不能在main()中聲明'Input()'。 –