2016-10-11 134 views
1

我想在我的程序中重複WHILE循環。我一直在這裏約4個小時,所以也許我只是想念一些東西。C++雖然循環不會重複

無論我輸入什麼內容,我都無法重複循環。此外,如果在提示中鍵入多個字符,則會跳過我所有後續循環中的所有7個字符。

然後,甚至更好的是,當輸入內容時,最後的變量甚至不會改變,希望你能幫助我解決上面列出的所有問題(越簡單越好)。但在這一點上,我只會採取一個。

cout << "Welcome. Input anything to start the sales reporting process.\n\n"; 
cin >> start; **//If I enter more than one character here it skips my other loops** 
if (start == start) 
{ 
    cout << "\n\nPlease enter the amount of each sale for Darwin when prompted, only enter one at a time.\n"; 
    cout << "When you are finished entering sales, input a '1'.\n\n"; 
    cout << "Enter a sale for Darwin: "; 
    cin >> darwinSale; 
    cout << endl << endl; 
    while(darwinSale =! 1) 
    { 
     cout << "Enter a sale for Darwin: "; 
     cin >> darwinSale; 
     if (darwinSale <= 50999) 
      darwinCom = darwinSale * 0.04; 
     if (darwinSale >= 51000 and darwinSale <= 125999) 
      darwinCom = darwinSale * 0.05; 
     if (darwinSale >= 126000 and darwinSale <= 200999) 
      darwinCom = darwinSale * 0.06; 
     if (darwinSale >= 201000) 
      darwinCom = darwinSale * 0.07; 
     darwinComTotal = darwinComTotal + darwinCom; 
     darwinTotal = darwinSale + darwinTotal; 
     cout << endl << endl; 
    } 
+3

嘗試將其更改爲while(darwinSale!= 1) – xDJR1875

+3

啊,是的,可怕的'=!'操作符。將其更改爲'!='。正如所寫,在'darwinSale =! 1','!'適用於'1',結果('0')被賦值給'darwinSale',產生一個0值並立即終止循環。 –

+1

另外你的if語句是多餘的,start == start應該總是等於true,因爲它與自身比較。 –

回答

7

在您的病情時,你實際上是檢查:

while(drawinSale = !1) 

和1個檢查1 == NULL至極返回0

而應該這樣做:

while(drawinSale != 1) 
+2

沒有NULL檢查 - '1'被轉換爲'true',然後否定爲'false',它被轉換爲'0'。 – molbdnilo

+0

謝謝!先生,我欠你的理智。 –