类的 C++ 初学者问题,可能是析构函数

标签 c++

下面是我当前家庭作业的代码。该程序使用一对重载运算符来提示用户输入三餐中每一餐的名称和卡路里计数,然后显示结果。第一次通过时一切正常,但是在第二次通过时它表现得有点奇怪。无论提示用户的第一餐是什么,都会保留上一轮的名称。该餐和接下来的三餐的所有其他值都可以输入,但第一餐的名称无论如何都保持不变。只是希望有人能帮助我指明正确的方向。谢谢!

#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;
}

最佳答案

问题是在 main 的 while 循环中有这一行:

cin >> response;

当用户按下回车键时,输入缓冲区中会留下一个尾随的换行符。由于您的函数 enterMealInfo 使用 getline,而 getline 正在寻找换行符,它会立即找到它正在寻找的内容,因此它不会提示用户。如果您在循环末尾添加这一行:

cin.ignore();

它将删除换行符。


附带说明一下,正如 Mr.Ree 在下面的评论中提到的,您的输入运算符有点奇怪。您将 istream 作为第一个参数,但不使用它。相反,您只需使用 cin。您应该使用传入的 istream,如 in.getline(name, 21);in >> calorie; 等...

关于类的 C++ 初学者问题,可能是析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388045/

相关文章:

c++ - 一位数字的 boost Spirit 解析器出现段错误

c++ - 什么是内部字段计数以及 SetInternalFieldCount 用于什么?

c++ - 使用枚举来专门化模板

c++ - 如何删除 C++ 中 UTF-8 字符串的最后一个字符?

c++ - Linux Mint x64 : Qt 5. 3.1 插件部署:不兼容的 qt 库

c++ - OpenCV 2.4.9读取空白图像

C++ 运算符 >> 重载

c++ - 通过引用传递 C++ 迭代器有什么问题?

c++ - 虚继承歧义函数

c++ - 更改 Cmake 文件以从源代码编译依赖项而不是使用 FIND_PACKAGE