2012-10-03 43 views
0

問題: 編寫一個程序,提示用戶輸入一定數量的1,2,5,10,20,50和100美元賬單。分別查詢用戶每個尺碼的數量。初始化程序列表和基於範圍的函數

我認爲我已經完成了代碼,但問題的最後部分陳述如下: 修改程序以使用向量的初始化程序列表,並使用基於範圍的for語句來遍歷向量。注意:這些C++ 11功能可用於g ++ - 4.6和Visual Studio 2012,但可能在其他編譯器上不可用。

我不明白最後一部分問的是什麼。希望有人能在這裏指出我的正確方向。這是一個C++類,我的書沒有最新版本。

對我的代碼格式的任何批評也會很棒!

這裏是我的代碼:

#include "std_lib_facilities_3.h" 
    vector<int>bills; 
    int bill; 
    int bill_tot(){ 
     int sum = 

    1*bills[0]+2*bills[1]+5*bills[2]+10*bills[3]+20*bills[4]+50*bills[5]+100*bills[6]; 
      return sum; 
     } 
    int main() 
    try{ 
     cout << "Enter the number of One Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of Two Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of Five Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of Ten Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of Twenty Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of Fifty Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 

     cout << "Enter the number of One Hundred Dollar Bills you have: "; 
     cin >> bill; 
     cout << '\n'; 
     bills.push_back(bill); 
     if (bill<0) 
      error("You can't have a negative value.\n"); 


     cout << "You have " << bills[0] <<" One Dollar Bills.\n"; 
     cout << "You have " << bills[1] <<" Two Dollar Bills.\n"; 
     cout << "You have " << bills[2] <<" Five Dollar Bills.\n"; 
     cout << "You have " << bills[3] <<" Ten Dollar Bills.\n"; 
     cout << "You have " << bills[4] <<" Twenty Dollar Bills.\n"; 
     cout << "You have " << bills[5] <<" Fifty Dollar Bills.\n"; 
     cout << "You have " << bills[6] <<" One Hundred Dollar Bills.\n"; 
     cout << "The value of all your Dollar Bills are $" << bill_tot() << endl; 
     keep_window_open(); 
    } 
    catch (runtime_error e) { 
     cout << e.what() << '\n'; 
     keep_window_open(); 
} 
+1

需要注意的是VC++ 2012 _不_支持初始化列表,但它支持範圍爲基礎的語句。 – ildjarn

回答

1

這說明代碼:

std::vector<int> denomination { 1,2,5,20,50,100}; // Initializer list for vector 
int sum = 0; 
int number; 
for (int bill : denomination) // range-based for loop 
{ 
    cout << "Enter the number of $" << bill << " you have: "; 
    cin >> number; 
    sum += number * bill; 
} 
+0

此外,@ user1715779:在測試您編寫的任何程序時,當它詢問您有多少賬單嘗試輸入「無」時,按回車鍵,然後查看會發生什麼情況...... – HostileFork

相關問題