C++;:检查对象是否为非空,以便使用cout索引每个输出 我在C++入门中工作了1.20。我正在尝试允许用户输入他们认为合适的任意多个项目,然后输出将是项目编号。是Item。对于每个条目,只要项不为空,no.将增加1

C++;:检查对象是否为非空,以便使用cout索引每个输出 我在C++入门中工作了1.20。我正在尝试允许用户输入他们认为合适的任意多个项目,然后输出将是项目编号。是Item。对于每个条目,只要项不为空,no.将增加1,c++,if-statement,while-loop,C++,If Statement,While Loop,我试图让我的程序检查用户是否输入了一个销售项目,以便为每个项目编制索引 我的代码如下: /* http://www.informit.com/title/0321714113 contains a copy of Sales_item.h * in the chapter 1 code directory. Copy that file to your pwd. Use it to * wrtie a program that reads a set of book sales transa

我试图让我的程序检查用户是否输入了一个销售项目,以便为每个项目编制索引

我的代码如下:

/* http://www.informit.com/title/0321714113 contains a copy of Sales_item.h
 * in the chapter 1 code directory. Copy that file to your pwd. Use it to
 * wrtie a program that reads a set of book sales transactions, writing each
 * transaction to the standard output.
 */

#include <iostream>
#include <string>
#include "IncC++files/1/Sales_item.h"

int main() {
  Sales_item item;  // call the item argument in Sales_item.h
  // prompt the user for the desired information to enter
  // std::string is used for long strings that run to multiple lines
  std::string my_phrase = "Input ISBN, number of copies sold, and"
                          " price sold and when complete enter C-d";
  std::cout << my_phrase << std::endl;
  /* defined while to allow users to continually enter in items until stopped
   * by C-d
   */
  for (int i = 0; i >= 0; ++i) {  // index the item number for the count
    while (std::cin >> item) {
      if (item != NULL) {
        // if item is not empty add one for the next item number
        i += 1;
        std::cout << "Item " << i << " is " << item << std::endl;
      } 
    }
  }
  return 0;
}
哪个操作员允许我检查
是否为非空


Linux系统下的< C++代码>代码> SaltJavaTy.H./C> >:

/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *  Pearson Education, Inc.
 *  Rights and Contracts Department
 *  75 Arlington Street, Suite 300
 *  Boston, MA 02216
 *  Fax: (617) 848-7047
*/ 

#ifndef SALESITEM_H
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before

public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
       lhs.same_isbn(rhs);
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

using std::istream; using std::ostream;

// assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same isbn
inline
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy lhs into a local object that we'll return
    ret += rhs;           // add in the contents of rhs 
    return ret;           // return ret by value
}

inline
istream& 
operator>>(istream& in, Sales_item& s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

inline
ostream& 
operator<<(ostream& out, const Sales_item& s)
{
    out << s.isbn << "\t" << s.units_sold << "\t" 
        << s.revenue << "\t" <<  s.avg_price();
    return out;
}

inline
double Sales_item::avg_price() const
{
    if (units_sold) 
        return revenue/units_sold; 
    else 
        return 0;
}


#endif
/*
*此文件包含Stanley B的“C++入门,第四版”中的代码。
*李普曼、何塞·拉乔伊和芭芭拉·E·穆奥,并被覆盖在
*该书中的版权和保修声明:
* 
*“Objectwrite,Inc.,Jose Lajoie和Barbara E.Moo于2005年版权所有。”
* 
* 
*“作者和出版商在本书的编写过程中非常谨慎,
*但不作出任何形式的明示或暗示保证,也不承担任何责任
*对错误或遗漏负责。不承担任何责任
*与本合同有关或由本合同引起的附带或间接损害
*使用此处包含的信息或程序。”
* 
*允许将此代码用于中的教育目的
*与本书的关联,在张贴或
*复制。本规范的任何商业用途都需要明确的书面说明
*出版商Addison Wesley Professional的许可
*Pearson Education,Inc.发送您的许可申请,明确说明
*您希望使用什么代码,以及以什么特定的方式来执行以下操作
*地址:
* 
*皮尔逊教育公司。
*权利和合同部
*阿灵顿街75号300室
*马萨诸塞州波士顿02216
*传真:(617)848-7047
*/ 
#ifndef销售项目
#定义销售项目
//此处定义了Sales_item类和相关函数
#包括
#包括
类别销售商品{
friend bool运算符==(常数销售项和,常数销售项和);
//与以前一样的其他成员
公众:
//添加了从字符串或istream初始化的构造函数
销售商品(const std::string和book):
isbn(图书),销售单位(0),收入(0.0){
销售项目(std::istream&is){is>>*此;}
friend std::istream&operator>>(std::istream&销售项目&);

friend std::ostream&operator
NULL
仅用于指针和指针。现在我们有了C++11,您应该使用它更好的对应项
nullptr

除此之外,您的输入运算符使用默认构造函数将对象设置为Black对象,因此您只需检查您的
项目是否与默认
销售项目相同即可

if (item != NULL)
变成

if (item != Sales_item())
编辑

您还需要定义一个默认构造函数,因为您已经提供了一个用户定义的构造函数

编辑2


您应该将构造函数分组在一起,而不是将它们放在类的另一侧。

无组织的代码有一个默认构造函数(我也错过了)。我不理解您定义默认构造函数的意思;但是,将
NULL
更改为
Sales\u item()
是需要的。问题:这篇文章是否侵犯了版权?@DieterLücking作者提供的代码可以在网上免费找到。此外,它还说,“允许将此代码用于与本书相关的教育目的,并在发布或复制时给予适当的引用。”我提到了这本书,在我的评论中提供了链接,作者的名字在文本简介中。
if (item != Sales_item())