2014-09-24 93 views
1

我必須編寫一個程序來模擬冰淇淋蛋筒供應商。用戶輸入圓錐的數量,並且對於每個圓錐,用戶輸入勺的數量,然後輸入每勺的風味(單個字符)。最後,總價格列出。對於定價,勺成本2.00,勺收費3.00並且每個舀收費.752之後。C++函數和循環

我在定價時遇到了問題。如果用戶只需要一個錐體,則顯示正確的價格。

/* 
* icecream.cpp 
* 
* Created on: Sep 14, 2014 
*  Author: 
*/ 

#include <iostream> 
#include <string> 

using namespace std; 

void welcome() { 
    cout << "Bob and Jackie's Ice Cream\n"; 
    cout << "1 scoop - $1.50\n"; 
    cout << "2 scoops - $2.50;\n"; 
    cout << "Each scoop after 2 - $.50\n"; 
    cout << "Ice Cream Flavors: Only one input character for each flavor.\n"; 
} 

bool checkscoops(int scoops) { 
    int maxscoops = 5; 

    if ((scoops > maxscoops) || (scoops < 1)) 

     return false; 
    else 
     return true; 
} 

bool checkcones(int cones) { 
    int maxcones = 10; 

    if ((cones > maxcones) || cones < 1) 
     return false; 
    else 
     return true; 
} 

int price(int cones, int numberofscoops) { 
    float cost = 0.00; 
    { 
     if (numberofscoops == 5) { 
      cost = cost + 5 + (.75 * 3); 
     } 
     if (numberofscoops == 4) { 
      cost = cost + 5 + (.75 * 2); 
     } 
     if (numberofscoops == 3) { 
      cost = cost + 5.75; 
     } 
     if (numberofscoops == 2) { 
      cost = cost + 5.00; 
     } 
     if (numberofscoops == 1) { 
      cost = cost + 2.00; 
     } 
    } 
    cout << "Total price is: " << cost << endl; 
} 
int buildcone(int numcones) { 
    char flav1, flav2, flav3, flav4, flav5; 
    int numberofscoops; 

    for (int i = 1; i <= numcones; i++) { 
     cout << "Enter the amount of scoops you wish to purchase. (5 max): "; 
     cin >> numberofscoops; 
     checkscoops(numberofscoops); 
     while (checkscoops(numberofscoops) == false) { 
      cout << "You are not allowed to buy more than 5 scoops and you " 
        "cannot buy less than one scoop. Please try again.\n"; 
      cout << "How many scoops would you like?(5 max): "; 
      cin >> numberofscoops; 
      checkcones(numberofscoops); 
     } 

     cout << "You are buying " << numberofscoops 
      << " scoops of ice cream.\n"; 
     if (numberofscoops == 5) { 
      cout << "Enter flavor 1: "; 
      cin >> flav1; 
      cout << "Enter flavor 2: "; 
      cin >> flav2; 
      cout << "Enter flavor 3: "; 
      cin >> flav3; 
      cout << "Enter flavor 4: "; 
      cin >> flav4; 
      cout << "Enter flavor 5: "; 
      cin >> flav5; 
      cout << " (" << flav1 << ")/" << endl; 
      cout << " (" << flav2 << ")" << endl; 
      cout << " (" << flav3 << ")" << endl; 
      cout << " (" << flav4 << ")" << endl; 
      cout << " (" << flav5 << ")" << endl; 
      cout << " \\" 
       << " /" << endl << " |" << endl; 
     } 
     if (numberofscoops == 4) { 
      cout << "Enter flavor 1: "; 
      cin >> flav1; 
      cout << "Enter flavor 2: "; 
      cin >> flav2; 
      cout << "Enter flavor 3: "; 
      cin >> flav3; 
      cout << "Enter flavor 4: "; 
      cin >> flav4; 
      cout << " (" << flav1 << ")" << endl; 
      cout << " (" << flav2 << ")" << endl; 
      cout << " (" << flav3 << ")" << endl; 
      cout << " (" << flav4 << ")" << endl; 
      cout << " \\" 
       << " /" << endl << " |" << endl; 
     } 
     if (numberofscoops == 3) { 
      cout << "Enter flavor 1: "; 
      cin >> flav1; 
      cout << "Enter flavor 2: "; 
      cin >> flav2; 
      cout << "Enter flavor 3: "; 
      cin >> flav3; 
      cout << " (" << flav1 << ")" << endl; 
      cout << " (" << flav2 << ")" << endl; 
      cout << " (" << flav3 << ")" << endl; 
      cout << " \\" 
       << " /" << endl << " |" << endl; 
     } 
     if (numberofscoops == 2) { 
      cout << "Enter flavor 1: "; 
      cin >> flav1; 
      cout << "Enter flavor 2: "; 
      cin >> flav2; 
      cout << " (" << flav1 << ")" << endl; 
      cout << " (" << flav2 << ")" << endl; 
      cout << " \\" 
       << " /" << endl << " |" << endl; 
     } 

     if (numberofscoops == 1) { 
      cout << "Enter a flavor: "; 
      cin >> flav1; 
      cout << " (" << flav1 << ")" << endl; 
      cout << " \\" 
       << " /" << endl << " |" << endl; 
     } 
    } 

    price(numcones, numberofscoops); 
} 

int main() { 
    int numberofcones; 
    int numberofscoops; 

    welcome(); 

    cout << "How many cones would you like?(10 max) "; 
    cin >> numberofcones; 
    checkcones(numberofcones); 
    while (checkcones(numberofcones) == false) { 
     cout << "You are not allowed to buy more than 10 cones and you cannot " 
       "buy less than one cone.  Please try again.\n"; 
     cout << "How many cones would you like?(10 max): "; 
     cin >> numberofcones; 
     checkcones(numberofcones); 
    } 
    cout << "You are buying " << numberofcones << " ice cream cones.\n"; 
    buildcone(numberofcones); 
} 
+1

你的'價格'函數根本不使用'cones'參數。也許你應該通過迄今爲止的總量。另外,難道你不能想到一個更好的方法來處理(每增加一勺75美分)?除此之外,你的'welcome()'函數,'price()'函數和你的問題描述都不同意2勺錐體的價格。 – 2014-09-24 01:14:48

回答

0

你的while()循環有缺陷。如下所示,將您的呼叫評論爲checkcones()。你已經在你的while()中調用checkcones()作爲條件,不需要再次評估,因爲這會將你發送到perma循環。您可以看到兩個while()語句,您可以將它們都註釋掉。

while (checkcones(numberofcones) == false) 
{ 
    cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone.  Please try again.\n"; 
    cout << "How many cones would you like?(10 max): "; 
    cin >> numberofcones; 

// THIS LINE IS THE PROBLEM :) 
// checkcones(numberofcones); 
} 

此修復程序後,程序開始工作,但定價失敗。你應該能夠用上面給出的答案來解決這個問題。

我也看看你是否可以弄清楚如何用成員和方法實現C++類,因爲你現在的方法非常「c」。快樂的編碼! :)

0

開始由price()返回值更改爲浮動,或功能將不能返回正確的成本。此外,由於cones不用於計算採購成本,我們沒有它作爲一個parameter

float price(int numberofscoops) 
{ 
    float total_cost = 0.0f; 

    if (numberofscoops == 1) { 
     total_cost = 2.0f; 
    } 
    else if (numberofscoops == 2) { 
     total_cost = 3.0f; 
    } 
    else if (numberofscoops > 2) { 
     total_cost = 5.0f + ((numberofscoops-2) * 0.75f); 
    } 

    return total_cost; 
} 

你的代碼可能有其他的問題,但我認爲這些變化會讓你繼續調試和自行修復代碼。