Getter function appears to be returning memory address instead of array

Hi everybody,

I'm working on an ice cream shop application to simulate purchasing ice cream, and then customizing it (type-cup/cone, size-small/large, & toppings). I've read in items and descriptions of those items from an inventory file, then storing it into a local array, and passing it into a class setter function in my header file. My header file consists of three classes, one being a class called Extras which stores the type (cone or cup), and toppings. I've created an array of size 5 for toppings, locally first--I'm using a do while loop to ask the user how many toppings they want (1-5), and then a for loop for the number of toppings. The program runs, no errors at all, but the problem I'm running into is either when I call the setToppings function, or call the getToppings function, I can't figure it out though. I've tested the program and the information is being stored into the local array, so I believe the problem is in the header file, but I'm not sure. It's setup how I've setup the items and descriptions set and get functions in the header file. I've tried to run through every possible scenario and can't figure it out, am I missing something? Any help would be greatly appreciated.


Full code can be found here:
https://pastebin.com/dwDyLaKL (main.cpp) lines 129-189, toppings lines 169-182
https://pastebin.com/32W3PrDC (data.h) lines 36-46, set toppings 97-101, get toppings 107-109.

I'm not sure if the code format worked or not so I included pastebin links.
Any help would be greatly appreciated. Thank you in advance!

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  MAIN.CPP
void customizeItems(IceCreamData data, Volume details, Extras customize) {

double treatCost;
int selectionType;
int selectionSize;
int toppingQty;
string type;
string typeSize;
string toppings[5];

cout<<"Would you like a cone or a cup?\n";
cout<<"1) Cone\n";
cout<<"2) Cup\n";
cin>>selectionType;

if(selectionType==1){
    type = "Cone";
    customize.setType(type);
} else if(selectionType==2){
    type = "Cup";
    customize.setType(type);
}

string treatType = customize.getType();

cout<<"Which size would you like?\n";
cout<<"1) Small (3 scoops)\n";
cout<<"2) Large (5 scoops)\n";
cin>>selectionSize;

if(selectionSize==1){
    typeSize = "Small (3 scoops)";
    details.setSize(typeSize);
} else if(selectionSize==2) {
    typeSize = "Large (5 scoops)";
    details.setSize(typeSize);
}

string sizeOfTreat = details.getSize();

do{
    cout<<"How many toppings would you like? (up to 5)? ";
    cin>>toppingQty;
    cin.ignore();
} while(toppingQty<1||toppingQty>5);

for(int i=0;i<toppingQty;i++){
        cout<<"Topping #"<<i+1<<": ";
        getline(cin, toppings[i]);
        customize.setToppings(toppings, i); //set toppings
    }

string* allToppings = customize.getToppings(); //get toppings, returns address
cout<<allToppings;

for(int i=0;i<5;i++){
   cout<<toppings[i];
}

menu(data, details, customize);
}

------------
DATA.H
------------
class Extras {
private:
	string type;
	string itemToppings[5];

public:
	void setType(string treatType);
	void setToppings(string toppings[], int i);
	string getType();
	string* getToppings();
};

void Extras::setType(string treatType){
    type = treatType;
}

void Extras::setToppings(string toppings[], int i){ //set the toppings
    for (int i = 0; i < 5; i++) {
		itemToppings[i] = toppings[i];
	}
}

string Extras::getType() {
    return type;
}

string* Extras::getToppings() { //get the toppings
    return itemToppings;
}
The program runs, no errors at all, but the problem I'm running into is either when I call the setToppings function, or call the getToppings function, I can't figure it out though.

If the program runs without errors, then it produces the correct output. That is the definition.

What exactly you cannot "figure out"?
If the program runs without errors, then it produces the correct output. That is the definition.

What exactly you cannot "figure out"?


I'm sorry, I meant it doesn't have any compiling errors when building or attempting to run, but does not produce the output desired. After all toppings have been entered I would the get function to return the toppings back to the user on lines 53 and 54.

On lines 53 and 54
string* allToppings = customize.getToppings();
is returning the memory address instead of the array value, and I'm not sure why that is. I've tried everything from making sure all variable names are matching when being passed to the get function, and I've tried while loops instead of for loops.
Program output I get:


How many toppings would you like? (up to 5): 2
Topping #1: chocolate chips
Topping #2: cherries
(calling get function here, line 53-54). 
0x6ee8d8
Last edited on
allToppings is a pointer. Pointers contain memory addresses. That's what you ask it to output on line 54, so that's what you get!

If you want the value at that memory location then (in c++) you have to dereference the pointer, either as *allToppings (which will just give you the first topping) or using array notation, allToppings[i], similar to that on lines 56-58.

There are languages which will, from context, treat a pointer on the RHS of an expression as an alias and not require explicit dereferencing. However, c++ isn't one of them.
Last edited on
On design, ask yourself why the Extras.itemToppings is private. You let everyone to modify it any way they like via the Extras::getToppings(). That breaks encapsulation.

If you want to control access to data, then do so thoroughly.


Similarly, what is the purpose of parameter i in the void Extras::setToppings(string toppings[], int i);?
You don't use that value in the function.

Furthermore, how does the function know that the parameter toppings has at least 5 elements?
Extras::getToppings was only meant to return the item toppings. Extras::setToppings was meant to set them--I was taught to make member variables private, and access them using public get and set functions, that's why I have the variable itemToppings listed as private.

The parameter i dose need to be removed, though--I haven't removed it yet though. When I was coding this I forgot that i had initialized int i inside the for loop. That's my bad, and does need to be removed--I just haven't yet.

During design I wanted it to be where the user can choose up to 5 toppings. Inside the for loop in Extras::setToppings I just have it set up to loop through 5 times, so when I was designing and coding this I figured that, lets say even if the user selects 3 toppings, then the other two elements of the array would be blank. Is this bad practice?

I'm sorry, I am still learning, thank you everyone for the replies, I do appreciate the help.

As far as the problem I listed, I did get it working--I used a for loop and looped through using array notation.

Thank you for all the help everyone, I'm still learning and I have learned a lot through the replies. Thank you!!
Topic archived. No new replies allowed.