0

以下は、私の現在の宿題のコードです。このプログラムは、いくつかのオーバーロードされた演算子を使用して、ユーザーに 3 回の食事それぞれの名前とカロリー数の入力を求め、結果を表示します。最初のパスではすべて正常に動作しますが、2 回目のパスでは少し奇妙な動作をします。ユーザーにプロンプ​​トが表示された最初の食事が何であれ、前のラウンドの名前が保持されます。その食事と次の 3 つの食事の他のすべての値を入力できますが、最初の食事の名前は何があっても変わりません。誰かが私を正しい方向に向けるのを手伝ってくれることを願っています。ありがとう!

#include <iostream>
#include <iomanip> 
#include <string>

using namespace std;

/*********************************************************************
File Name: meal.cpp
Author: Neal Rodruck
Date: 7/8/12
Purpose: To provide intergallactic travelers a means of measuring their
         daily caloric intake.
*********************************************************************/

class Meal
{
private:
    string name;
    int calorie;
public:
    //Class constructors
    Meal() : name("Meal 1"), calorie(0)
    {}
    Meal(string name, int calorie) : name(name), calorie(calorie)
    {
        while (calorie < 1)
        {
            cout << "Please enter a caloric value greater than 0!: ";
            cin >> calorie;
        }
    }

    //Class destructor
    Meal::~Meal()
    {}

    //get functions
    string getName() { return name; }
    int getCalorie() { return calorie; }

    //set functions
    void setName(string n) { name = n; }
    void setCalorie(int c) { calorie = c; }

    //Overloaded operators
    friend ostream &operator<<(ostream &out, Meal m);
    friend istream &operator>>(istream &in, Meal &m);
    friend Meal operator+(Meal a, Meal b);
};

//Calculate two or more meal objects to obtain daily total
Meal operator+(Meal a, Meal b)
{
    return Meal("Daily Total", a.calorie + b.calorie);
}

//Prompt user for name and calorie information 
//for Meal object as well as test for greater 
//than zero calorie total
istream &operator>>(istream &in, Meal &m)
{
    char name[21];
    int calorie = 0;

    cout << "Enter name: ";
    cin.getline(name, 21);  
    cout << "Enter calories: ";
    in >> calorie;

    while (calorie < 1)
    {
        cout << "Please enter a caloric value greater than 0!: ";
        cin >> calorie;
    }

    m.setName(name);
    m.setCalorie(calorie);
    cin.ignore();

    return in;
}

//Display object information
ostream &operator<<(ostream& out, Meal m)
{
    out << "Name: " << m.name << " Calories: " << m.calorie;
    return out;
}

//function prototypes
void makeNull(Meal& breakfast, Meal& lunch, Meal& dinner);
void introduction();
void display(Meal b, Meal l, Meal d, Meal t);
void end();
void enterMealInfo(Meal& breakfast, Meal& lunch, Meal& dinner);

int main()
{
    //Display introductory message
    introduction();

    //Meal Objects
    Meal breakfast;
    Meal lunch;
    Meal dinner;

    //Capture user response
    char response = ' ';

    //Use loop to allow user to enter information for 
    //more than one day
    while (response != 'n')
    {


        //Prompt user for meal information
        enterMealInfo(breakfast, lunch, dinner);

        //Use information captured to create Daily Total meal object
        Meal total(breakfast + lunch + dinner);

        //Display results
        display(breakfast, lunch, dinner, total);

        //Prompt user for more input
        cout << "Would you like to check again? Please selct \"y\" or \"n\": ";
        cin >> response;
        response = tolower(response);
    }

    //Display exit message
    end();

    return 0;
}

//Display introductory message
void introduction()
{
    cout << "Welcome to The Voyager Trek!";
    cout << "\nPlease use this app to keep track of your daily caloric intake!\r\n";
}

//Display meal and summary information
void display(Meal b, Meal l, Meal d, Meal t)
{
    cout << "\n" << left << setw(20) << "Meal" << right << setw(20) << "Calories";
    cout << "\n" << left << setw(20) << b.getName();
    cout << right << setw(20) << b.getCalorie();
    cout << "\n" << left << setw(20) << l.getName();
    cout << right << setw(20) << l.getCalorie();
    cout << "\n" << left << setw(20) << d.getName();
    cout << right << setw(20) << d.getCalorie();
    cout << "\n" << "----------------------------------------";
    cout << "\n" << left << setw(20) << t.getName();
    cout << right << setw(20) << t.getCalorie() << "\n";
}

//Display exit message
void end()
{
    cout << "\r\nThank you for using our app, goodbye!\r\n";
    system("pause");
}

//Using meal objects passed by reference this function 
//will populate appropriate objects with name and calorie
//information
void enterMealInfo(Meal& breakfast, Meal& lunch, Meal& dinner)
{
    cout << "\r\nWhat did you have for breakfast?\r\n";
    cin >> breakfast;
    cout << "What did you have for lunch?\r\n";
    cin >> lunch;
    cout << "What did you have for dinner?\r\n";
    cin >> dinner;
}
4

2 に答える 2

4

問題は、main の while ループに次の行があることです。

cin >> response;

ユーザーが Enter キーを押した時点から、末尾の改行文字が入力バッファーに残されます。関数enterMealInfoは getline を使用し、getline は改行文字を探しているため、探しているものをすぐに見つけて、ユーザーにプロンプ​​トを表示しません。ループの最後に次の行を追加すると、次のようになります。

cin.ignore();

改行文字を削除します。


ちなみに、Mr.Ree が下のコメントで言及したように、あなたの入力演算子は少し奇妙です。最初のパラメーターとして istream を使用しますが、使用しません。代わりに、単に使用しますcin。渡された istream などを使用する必要がありin.getline(name, 21);ますin >> calorie;...

于 2012-07-09T03:16:52.637 に答える
0

確かに、入力を同期するのを忘れていました。

これを試して:

...
while (response != 'n')
{
    //Sync the input stream 
    cin.sync();

    //Prompt user for meal information
    enterMealInfo(breakfast, lunch, dinner);
...
于 2012-07-09T01:34:11.300 に答える