Xcode可以混合缓冲和非缓冲输入功能吗?

时间:2016-02-25 21:43:34

标签: c++ xcode bufferedinputstream

我创建了这个简单的程序来练习使用类。我确定我使用这门课的方式有些错误,但我刚刚开始了解它们,所以我还没有学习所有的约定和礼仪。我的主要问题是,Xcode可以具有缓冲功能和其他非缓冲功能吗?我希望我的函数void InputValuesAndDisplayTotals();被缓冲并且我的函数void ManuallyAddCoinsAndDisplayTotals();是非缓冲的(基本上,点击一个键并且不使用回车键立即处理该字符)。这可能吗?谢谢你提前。

#include <iostream>
#include <iomanip>
using namespace std;

class Coin
{
private:

    const float PENNYVALUE   = 0.01;
    const float NICKELVALUE  = 0.05;
    const float DIMEVALUE    = 0.10;
    const float QUARTERVALUE = 0.25;

    int PennyInput=0;
    int NickelInput=0;
    int DimeInput=0;
    int QuarterInput=0;

public:

    void InputValuesAndDisplayTotals();
    void ManuallyAddCoinsAndDisplayTotals();
    void Total();
};

int main()
{
    int Choice;

    Coin Count;

    cout << "1 to enter total coin counts, 2 to manually count coins: ";
    cin >> Choice;

    if (Choice==1)
    {
        Count.InputValuesAndDisplayTotals();
        Count.Total();
    }

    else
    {
        Count.ManuallyAddCoinsAndDisplayTotals();
    }

    cout << endl;
}

void Coin::InputValuesAndDisplayTotals()
{
    cout << fixed << setprecision(2) << endl;

    cout << "Input penny count:   ";
    cin >> PennyInput;
    cout << "Total penny value:   $" << PENNYVALUE*PennyInput;
    Total();
    cout << endl;

    cout << "Input nickel count:  ";
    cin >> NickelInput;
    cout << "Total nickel value:  $" << NICKELVALUE*NickelInput;
    Total();
    cout << endl;

    cout << "Input dime count:    ";
    cin >> DimeInput;
    cout << "Total dime value:    $" << DIMEVALUE*DimeInput;
    Total();
    cout << endl;

    cout << "Input quarter count: ";
    cin >> QuarterInput;
    cout << "Total quarter value: $" << QUARTERVALUE*QuarterInput;
    Total();
}

void Coin::ManuallyAddCoinsAndDisplayTotals()
{
    char Choice2;

    cout << "\n'1' for penny,\n'2' for nickel,\n'3' for dime,\n'4' for quarter,\n'q' to quit\n";

    do
    {
        cout << "\nInput: ";
        cin >> Choice2;

        if (Choice2=='1')
            ++PennyInput;

        if (Choice2=='2')
            ++NickelInput;

        if (Choice2=='3')
            ++DimeInput;

        if (Choice2=='4')
            ++QuarterInput;

        cout << endl;

        cout << "Pennies:  " << PennyInput   << endl;
        cout << "Nickels:  " << NickelInput  << endl;
        cout << "Dimes:    " << DimeInput    << endl;
        cout << "Quarters: " << QuarterInput << endl;

        Total();
    }
    while (Choice2!='q' && Choice2!='Q');
}

void Coin::Total()
{
    cout << "\nTotal amount: $";

    cout << fixed << setprecision(2) << (PENNYVALUE*PennyInput)+(NICKELVALUE*NickelInput)+(DIMEVALUE*DimeInput)+(QUARTERVALUE*QuarterInput);

    cout << "\n";
}

0 个答案:

没有答案