Trouble printing data from user class type array?

I am new to classes, and I am trying to create a program that will display a table of data using a user class type array. I believe that it is currently printing the address of the data, not the value. I could definitely be wrong, but I feel like that's what is printing. Do I need to use a pointer here, or how do I fix this issue?

I will attach code for Invoice.h, Invoice.cpp, and my main program.
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
#pragma once
#ifndef INVOICE_H
#define INVOICE_H
#include "stdafx.h"
#include<iostream>
#include <string>

using namespace std;

class Invoice
{
private:
	int partNum;
	string partDes;
	int quantity;
	double price;

public:
	Invoice();
	Invoice(int partNum, string partDes, int quantity, double price);
	int getNum() const;
	string getDes() const;
	int getQuan() const;
	double getPrice() const;
	~Invoice();
};
#endif


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
#include "stdafx.h"
#include<iostream>
#include <string>
#include "Invoice.h"


using namespace std;


Invoice::Invoice()
{
}


Invoice::~Invoice()
{
}

Invoice::Invoice(int partNum, string partDes, int quantity, double price)
{
	partNum = 0;
	partDes = "";
	quantity = 0;
	price = 0.0;
}

int Invoice::getNum() const
{
	return partNum;
}

string Invoice::getDes() const
{
	return partDes;
}

int Invoice::getQuan() const
{
	return quantity;
}

double Invoice::getPrice() const
{
	return price;
}


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
// U14.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <iomanip>
#include "Invoice.h"

using namespace std;


int main()
{
	const int NUM_ITEMS = 8;
	Invoice invoices[] =
	{
		Invoice(83, "Electric sander", 7, 57.98),
		Invoice(24, "Power saw", 18, 99.99),
		Invoice(7, "Sledge hammer", 11, 21.5),
		Invoice(77, "Hammer", 76, 11.99),
		Invoice(39, "Lawn mower", 3, 79.5),
		Invoice(68, "Screwdriver", 106, 6.99),
		Invoice(56, "Jig saw", 21, 11.00),
		Invoice(3, "Wrench", 34, 7.5)
	};

	cout << setw(4) << "Part Number\t";
	cout << setw(8) << "Part Description\t";
	cout << setw(3) << "Quantity\t";
	cout << setw(5) << "Price\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << invoices[i].getNum();
	}

    return 0;
}

Hello stormbot,

At first look I do not see much that is wrong except the ctors.

What you have between the {}s of the overloaded ctor should be in the default ctor except the "partDes". As a "std::string" it is empty when defined and does not need initialized. The same is true for a vector. Every other variable does need to be initialized including arrays. This also is true for variables outside of the class.

What you have in the overloaded ctor is not going to work because the variable names in the parameters of the function definition are the same as the class names. The code as it should be is partNum = partNum;. The question is which part number are you using. Two choices here would be to make the variable names in the class different from the parameter name as "m_partNum" with the "m_" letting you know that it is part of the class. And for a struct I like to prefix the variable names with "s_". The other option is to change the names in the function definition so they are different from the class variable names.

The "stdafx.h" as you and I both know that you need this for VS projects, but it is best left out when posting code here.

When you start a new project You have the first window where you enter the project name. After you press enter the next window comes up. Instead of pressing "finish" press "next" in about the middle of the window just left of center there is a check box that says "Empty project. Check this box then press finish. You will have a project set up, but without the wizard generated code that you are use to. I usually use the key combination of "Ctrl + Shift + A" to add a new file to the project. The pop up window will have the choice os a ".cpp" or ".h" files. This way you will not need the "stdafx.h" and you will have to put all the header files that you will need pluss all the code yo will need. The added advantage is that the project will keep track of all the files that need to be put together at compile time.

Hope that helps,

Andy
First, DO NOT put
using namespace std;
in your header files. Ever.

People include header files trusting them to not do this. To NOT pollute the namespace by dragging in everything from std in every subsequent header they include. It's a really bad idea to put that in your header files.

Anyway, I see that you never set any values in your Invoice objects. Your constructor doesn't set any values in the Invoice object. All you do is set the values of the parameters passed in to zero. Your actual Invoice class doesn't get any values set.

Do not name your construction parameters the same as your class variables. If you hadn't done that, you would have seen the problem yourself.
How would I set my array data assigned to my Invoice objects?
Hello stormbot,

As I said earlier you constructors should look like this and this is what I did to go along with what Repeater said
Do not name your construction parameters the same as your class variables. If you hadn't done that, you would have seen the problem yourself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Invoice::Invoice()
{
	m_partNum = 0;
	m_quantity = 0;
	m_price = 0.0;
}

Invoice::~Invoice()
{
}

Invoice::Invoice(int partNum, std::string partDes, int quantity, double price)
{
	m_partNum = partNum;
	m_partDes = partDes;
	m_quantity = quantity;
	m_price = price;
}

This is just one part of the fix.

The other thing I did was in main in the for loop I added to the "cout" statement std::cout << invoices[i].getNum() << ' ';. This puts a space between each number so you can actually see the numbers instead of being all run together.

In addition to what Repeater said you do not need to put the "#include" files in the header file they should only be in the ".cpp" files. And for the using namespace std; line that is best not to use at all you should read this:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Other than the ctors needing changed and the "cout" in the for loop in main the program runs fine. You array is working storing all the information the way it should and if you choose to print out more information you could.

Hope that helps,

Andy
Hello stormbot,

Played around with the final output a bit and came up with this:
Part Number     Part Description        Quantity        Price
    83          Electric sander             7          $ 57.98
    24          Power saw                  18          $ 99.99
     7          Sledge hammer              11          $  21.5
    77          Hammer                     76          $ 11.99
    39          Lawn mower                  3          $  79.5
    68          Screwdriver               106          $  6.99
    56          Jig saw                    21          $    11
     3          Wrench                     34          $   7.5


 Press Enter to continue

An idea of what could be done.

Andy
Topic archived. No new replies allowed.