2014-09-24 165 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; 
} 

你的代码可能有其他的问题,但我认为这些变化会让你继续调试和自行修复代码。