cannot run my code

hi:

I am trying an example code from a book (C++Primer. I do not get any error but when I hit the Build and Run option it does not do anything.

Could you help me?

Thanks!

#include <iostream>
#include "Sales_item.h"

int main()
{
Sales_item book;

std::cin>>book;
std::cout<<book<<std:endl;
return 0;
}
You forgot a colon.
std:endl should be std::endl
thanks!

I typed the colon but still the same problem. I suspect that the non-visible error is due to the header and how I added it.

To the right of the Build and Run icon there is a two-options menu-release and debug. When is set to Release I see a red box indicating an error but I do not know how to proceed.

I went to Build options, selected Search Directories, selected Apend target options to Project options, click add and selected this default set path C:\Users\Filosofo\Documents\PROGRAMAS EN C++\1.5.1 The sales item class\ . Then clicked ok.

Any help will be very much appreciated.




try searching ur error in Sales_item.h because this code looks fine to me(except the semicolon that yay295 already pointed out)
Personally I would link the contents of your Sales_item.h so that we can help you better.
These are the contents of Sales_item.h

#include <iostream>
#include "Sales_item.h"

int main()
{
Sales_item book;

std::cin>>book;
std::cout<<book<<std::endl;
return 0;
}

and this are from sales_item.h.cpp


#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
//ctor
}

Sales_item.h::~Sales_item.h()
{
//dtor
}

I did nothing on the code above, it was generated automatically.

The book says I have to add this code to the working directory, but I do not where exactly to copy it:

/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee 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 Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/

/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);

// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold;
double revenue;
};


Thanks!


Last edited on
Please use code tags to format your code. http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Sales_item.h"

int main()
{
Sales_item book;

std::cin>>book;
std::cout<<book<<std::endl;
return 0;
}


and this are from sales_item.h.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
//ctor
}

Sales_item.h::~Sales_item.h()
{
//dtor
}


I did nothing on the code above, it was generated automatically.

The book says I have to add this code to the working directory, but I do not where exactly to copy it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee 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 Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/

/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);

// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold;
double revenue;
};

Last edited on
Do you have to write the definitions yourself?

1
2
3
4
5
6
7
8
9
10
11
#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
//ctor
}

Sales_item.h::~Sales_item.h()
{
//dtor
}


This code should be in a file called
Sales_item.cpp
and look like this

1
2
3
4
5
6
7
8
9
10
11
#include "Sales_item.h"

Sales_item::Sales_item()
{
//ctor
}

Sales_item::~Sales_item()
{
//dtor
}


Along with the other definitions.
This is the only code I copied from the book:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Sales_item.h" 

int main()

Sales_item book;

    std::cin>>book;
    std::cout<<book<<std::endl;
    return 0;
}






After that I created a class called Sales_Item and Codeblocks created these two:
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Sales_item.h"


and this one:



#include "Sales_item.h.h"

Sales_item.h::Sales_item.h()
{
//ctor
}

Sales_item.h::~Sales_item.h()
{
//dtor
}



The author asks me to add this file to the current directory. However, I do not understand what exactly he means.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/

#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>

class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0) { }
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);

// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold;
double revenue;
};



Thanks!
Topic archived. No new replies allowed.