所以我昨天剛開始使用 C ,我有相當多的 java 經驗,所以這就是 idk 的原因,我嘗試運行這段代碼,但由于某種原因,while 回圈沒有回圈,我嘗試將 if break 陳述句從更改ask==false為ask=false,這只是以無限回圈結束,甚至沒有接受用戶輸入。
這是代碼:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double raduis;
const double pi = 3.14;
bool ask;
while(true){
cout << "Enter the raduis of the circle:"<< endl;
cin >> raduis;
double circ = 2*pi*raduis;
double area = pi*pow(raduis,2);
cout << "The Circumference of the circle is: "<< circ <<endl;
cout << "The Area of the circle is: "<< area<<endl;
cout <<"Would you like to run again?"<< endl;
cin >> ask;
if(ask==false){
break;
}
}
}
我嘗試使用或值將其更改bool為一個char值,但無濟于事。"y""n"
編輯:好的,所以我已經解決了這個問題,按照我輸入的@zdf 的建議cin >> boolalpha >> ask;,它現在可以正常作業了。
uj5u.com熱心網友回復:
在輸入之前呼叫 clear oncin并確保輸入僅為0or 1,
從cppreference:
如果
visbool和boolalpha未設定的型別,則如果要存盤的值是0?,false則存盤,如果要存盤的值是1,true則存盤,對于任何其他值std::ios_base::failbit都分配err并true存盤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527179.html
上一篇:如何縮短許多while回圈?
