2013-12-14 64 views
-1

我已經做了一個基本的做while循環,我想輸出汽車租賃在左邊和右邊的屏幕voucherno。如果用戶想要重複這個過程,那麼憑證號應該增加1.另一件事是我有的表達式有什麼問題, 它表示期望在=標記之前的表達式。如何在C++中每次運行一個循環時將變量增加1?


do { 
     unsigned short voucherno=0; 
     char processanother; 
     cout<<"CAR HIRE"<<setw(4)<<setfill('0')<<"Voucher Number:"<<voucherno++; 
     cout<<"Repeat again to test the loop Y/N?"; 
     cin>>processanother; 
    } 
    while(processanother!=='y'||process!=='Y'); 
+8

使processanother外 「做,而」 與改變!==來!= – qwr

+0

@qwr你的意思是'全球 – dkrikun

回答

6

使用for環或申報外循環的變量。請注意,for循環中的條件實際上可以是任何條件,它不需要查看其他兩個表達式使用的相同變量。

char processanother = 'y'; 
for (unsigned short voucherno=0; 
    processanother=='y' || processanother =='Y'; 
    ++voucherno) { 
    std::cout << ... 
    std::cin >> processanother; 
} 

您已經編寫代碼的方式,每次迭代創建一個新的變量voucherno

而@qwr說:運營商是!=,而不是!==。但我相信你想要==

0

如果你在do while循環中定義了voucherno,那麼voucherno是一個局部變量。每個循環它被定義爲0.所以你不會得到實際的計數。因此,在do-while循環之前定義voucherno。

在C++中,如果您想要測試兩個變量是否相等,則使用==運算符。如果你想測試它們是否不同,你可以使用!=而不是!==!==是非法的。

+0

感謝voucherno'工作完美 – user3102359

0
unsigned short voucherno=0; 
do { 

    char processanother; 
    cout<<"CAR HIRE"<<setw(4)<<setfill('0')<<"Voucher Number:"<<voucherno++; 
    cout<<"Repeat again to test the loop Y/N?"; 
    cin>>processanother; 
} 
while(processanother=='y'||process=='Y'); 

你的代碼有兩個錯誤。 1. voucherno變量是在side循環中聲明的,所以它不會顯示你遞增的值,每次循環時它都會被聲明並賦值爲0,所以它將顯示爲零(0) 2.當條件存在時另一個錯誤在C++中沒有任何操作符像「!==」,就像您使用的那樣。如果你想檢查相等性,那麼使用==,如果你想檢查不等於那麼使用!=。

感謝

+0

謝謝,工作完美。 – user3102359

相關問題