2014-10-08 205 views
0

所以我的編程能力有點生疏,除了我過去參加的大學課程之外,沒有任何實際經驗。我正在爲一個課程開發一個程序,但遇到了一個障礙;我無法弄清楚如何在循環外的For循環內使用變量的值。這裏是我引用的代碼:如何使用C++循環外的For循環中的變量?

#include "iostream" 

using namespace std; 

int want, have, need; 
int counter = 0; 

char response, cont; 

int diff(int want, int have); 

int main(){ 
cout << "Welcome!\n"; 
cout << "This program will help you reach your money saving goals!\n"; 
cout << "Would you like to use this program? (y/n)\n"; 
    cin >> response; 
    while (response == 'y'){ 
     cout << "Please enter all amounts in whole dollars only!\n"; 
     cout << "Please enter the amount of money you would like to have saved: $"; 
     cin >> want; 
     cout << "\nPlease enter the amount of money you currently have saved: $"; 
     cin >> have; 
     if (have >= want){ 
      cout << "You have already reached or exceeded your goal, this program will not be able to help you!\n"; 
      system("Pause"); 
      return 0; 
     } 
     cout << "\nYou need to save $" << diff(want, have) << " more money to reach your goal!\n"; 
     cout << "Would you like me to help you with a savings plan?"; 
     cin >> cont; 
     while (cont == 'y'){ 
      int menu; 
      cout << "Please select from the following options: \n"; 
      cout << "1 - Daily Saving Plan\n"; 
      cout << "2 - Weekly Saving Plan\n"; 
      cout << "3 - Monthly Saving Plan\n"; 
      cout << "Enter the number associated with your choice: \n"; 
      cin >> menu; 

      switch (menu){ 
       case 1: { 
        int daily; 
        cout << "You have chosen the Daily Savings Plan\n"; 
        cout << "How much money can you save every day? $"; 
        cin >> daily; 
        for (int x = daily; x < need; x++){ 
         daily = daily + daily; 
         counter++; 
        } 
        cout << "\nIt will take you " << counter << " days to reach your goal!\n"; 
        break; 
       } 
       case 2: { 
        int weekly; 
        cout << "You have chosen the Weekly Savings Plan\n"; 
        cout << "How much money can you save every week? $"; 
        cin >> weekly; 
        for (int x = weekly; x < need; x++){ 
         counter++; 
        } 
        cout << "\nIt will take you " << counter << " weeks to meet your goal!\n"; 
        break; 
       } 
       case 3: { 
        int monthly; 
        cout << "You have chosen the Monthly Savings Plan\n"; 
        cout << "How much money can you save every month? $"; 
        cin >> monthly; 
        for (int x = monthly; x < need; x++){ 
         monthly = monthly + monthly; 
         counter++; 
        } 
        cout << "\nIt will take you " << counter << " months to reach your goal!\n"; 
        break; 

       } 
       default: cout << "You made an invalid selection"; 

        cout << "Would you like to look at a different saving plan? (y/n)\n"; 
        cin >> cont; 
      } 
     } 
    } 
} 

int diff(int want, int have){ 
return want - have; 
} 

所以,當我運行程序,一切運行正常,但計數器的值在最後cout聲明始終顯示爲「0」。

我明白爲什麼它會這樣做,我想..這是因爲循環外有「int counter = 0」聲明,所以我假設在循環結束後它會回到那個值。

如果我不啓動計數器變量,我得到一個錯誤,並且如果我在循環中聲明瞭值,我會在cout語句中出現一個錯誤,正如我上面所述。

我甚至不確定我的循環結構是否正確...基本上我希望它自己添加周變量,直到總數爲x = need。我也想捕獲它需要多少次迭代,然後輸出它作爲週數。希望這一切都有道理;任何和所有的幫助表示讚賞。

+0

重現問題的郵政編碼。 – 2014-10-08 16:24:22

+7

不,在循環後'counter'不會回零。如果它爲零,那是因爲它從來沒有增加過。在循環開始之前檢查'weekly'和'need'的值。 – 2014-10-08 16:24:46

+0

對於初學者,我會嘗試在函數的開頭聲明計數器外部開關。在循環開始之前應該進行初始化:counter = 0; – user3621602 2014-10-08 16:25:14

回答

0

看起來你想要做的事情可以用ceil(double(need/weekly))完成,並且需求除以每週。

在完成for循環後,您在環路外的聲明不會影響您cout的值。

至於你的問題,它看起來像你從來沒有初始化need讓你爲未定義是在你的程序的C++ 11比不小於或等於0

0

草圖for循環從未迭代。

char prompt(std::initializer_list<const char*> message, std::initializer_list<const char*> question, std::initializer_list<char> options) { 
    for(auto msg:message) 
    std::cout << msg; 
    while(true) { 
    char response; 
    for(auto q:question) 
     std::cout << q; 
    std::cin >> response; 
    for (char option:options) { 
     if (response == option) 
     return response; 
    } 
    } 
} 
int prompt(std::initializer_list<const char*> message, std::initializer_list<const char*> question, int min = 0, int max = std::numeric_limits<int>::max()) { 
    for(auto msg:message) 
    std::cout << msg; 
    while(true) { 
    int response; 
    for(auto q:question) 
     std::cout << q; 
    std::cin >> response; 
    if (response >= min && response <= max) 
     return response; 
    } 
    } 
} 
void saving(const char* plan, const char* unit, const char* units, int target) { 
    int daily = prompt(
    {"You have chosen the ", plan, "\n"}, 
    {"How much money can you save every ", unit, "? $"}, 
    1 // min saving of 1$ per unit time to prevent infinite loops 
); 
    std::cout << "\n"; 
    int counter = 0; 
    int amount_saved = 0; 
    while (amount_saved < target) { 
    ++counter; 
    amount_saved += daily; 
    } 
    if (counter != 1) 
    std::cout << "It will take you " << counter << " " << units << " to reach your goal\n"; 
    else 
    std::cout << "It will take you " << counter << " " << unit << " to reach your goal\n"; 
} 

int main() { 
    while(
    prompt(
     {"Welcome!\nThis program will help you reach your money saving goals!\n"}, 
     {"Would you like to use this program? (y/n)\n"}, 
     {'y', 'n'} 
    ) == 'y' 
) 
    { 
    int want = prompt({"Please enter all amounts in whole dollars only!\n"}, 
     {"Please enter the amount of money you would like to have saved?"}); 
    int have = prompt({"\n"}, {"Please enter the amount of money you currently have saved?\n"}); 
    std::cout << "\n"; 
    if (have >= want) { 
     std::cout << "You win!\n"; 
     system("Pause"); // ick 
     return 0; 
    } 
    std::cout << "You need to save $" << (have-want) << " more money to reach your goal!\n"; 
    while('y' == prompt(
     {}, 
     {"Would you like me to help you with a savings plan? (y/n)"}, 
     { 'y', 'n' } 
    )) { 
     char menu = prompt(
     { 
      "Please select from the following options: \n", 
      "1 - Daily Saving Plan\n", 
      "2 - Weekly Saving Plan\n", 
      "3 - Monthly Saving Plan\n" 
     }, 
     {"Enter the number associated with your choice: \n"}, 
     { '1', '2', '3' } 
     ); 
     switch (menu) { 
     case '1': { 
      saving("Daily Saving Plan", "day", "days", have-want); 
     } break; 
     case '2: { 
      saving("Weekly Saving Plan", "week", "weeks", have-want); 
     } break; 
     case '3': { 
      saving("Monthly Saving Plan", "month", "months", have-want); 
     } break; 
     } 

    } 
    } 
}