How to pull data from a function?

Hello!

I'm trying to run a function and return the results of the function so that I can compare the resulting integer to a class of items. I've coded the sort of "questionnaire" part to get the numbers but I'm a bit confused on how to take the results. So, once it returns treeData, I want it to keep the data in the main function.

I'm also getting an error when I try to compile telling me that there's multiple definitions of my function treeForm() but it's only listing the one instance of it.

Any help would be appreciated. This is what I've got going on:

Main.cpp
1
2
3
4
5
6
7
8
#include "src/treedata.cpp"

int main()
{
    int treeForm();

    treeForm();
}



treedata.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include "../include/treedata.h"


int treeForm()
{
    int leafChoice=0;
    int veinChoice=0;
    int edgeChoice=0;
    int barkChoice=0;

        std::cout << "Welcome to Tree ID!" << std::endl;
Menu1:
        std::cout << "Please choose the type of leaf to get started." << std::endl << std::endl;
        std::cout << "1. Hand Shaped Leaves" << std::endl;
        std::cout << "2. Spear Shaped Leaves" << std::endl;
        std::cout << "3. Heart Shaped Leaves" << std::endl;
        std::cout << "4. Round Leaves" << std::endl;
        std::cout << "5. Needles" << std::endl;
        std::cout << "6. Quit" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> leafChoice;
        std::cout << std::endl;

    if (leafChoice == 6) return 0;
	while ((leafChoice < 1) || (leafChoice >6)) {
		std::cout << "Invalid input" << std::endl << std::endl;
		std::cout << "Please choose the type of leaf to get started." << std::endl << std::endl;
		std::cout << "1. Hand Shaped Leaves" << std::endl;
		std::cout << "2. Spear Shaped Leaves" << std::endl;
		std::cout << "3. Heart Shaped Leaves" << std::endl;
		std::cout << "4. Round Leaves" << std::endl;
		std::cout << "5. Needles" << std::endl;
		std::cout << "6. Quit" << std::endl << std::endl;
		std::cout << "Enter choice: ";
		std::cin >> leafChoice;
        std::cout << std::endl;
    if (leafChoice == 6) return 0;
	}
Menu2:
        std::cout << "Now let's take a look at the veins, are they:" << std::endl << std::endl;
        std::cout << "1. Netlike?" << std::endl;
        std::cout << "2. Parallel?" << std::endl;
        std::cout << "3. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> veinChoice;
        std::cout << std::endl;

    if (veinChoice == 3) goto Menu1;
    while ((veinChoice < 1) || (veinChoice >3)) {
        std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "Now let's take a look at the veins, are they:" << std::endl << std::endl;
        std::cout << "1. Netlike?" << std::endl;
        std::cout << "2. Parallel?" << std::endl;
        std::cout << "3. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> veinChoice;
        std::cout << std::endl;
    if (veinChoice == 3) goto Menu1;
    }
Menu3:
        std::cout << "How about the edges of the leaves, are they?" << std::endl << std::endl;
        std::cout << "1. Smooth edges" << std::endl;
        std::cout << "2. Toothed edges" << std::endl;
        std::cout << "3. Lobed edges" << std::endl;
        std::cout << "4. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> edgeChoice;
        std::cout << std::endl;

    if (edgeChoice == 4) goto Menu2;
    while ((edgeChoice < 1) || (edgeChoice > 4)){
        std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "How about the edges of the leaves, are they?" << std::endl << std::endl;
        std::cout << "1. Smooth edges" << std::endl;
        std::cout << "2. Toothed edges" << std::endl;
        std::cout << "3. Lobed edges" << std::endl;
        std::cout << "4. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> edgeChoice;
        std::cout << std::endl;
    if (edgeChoice == 4) goto Menu2;
    }
        std::cout << "What does the bark look like?" << std::endl << std::endl;
        std::cout << "1. Smooth" << std::endl;
        std::cout << "2. Scales" << std::endl;
        std::cout << "3. Plates" << std::endl;
        std::cout << "4. Peeling strips" << std::endl;
        std::cout << "5. Shed Ribbon" << std::endl;
        std::cout << "6. Fibrous" << std::endl;
        std::cout << "7. Curved Ridges" << std::endl;
        std::cout << "8. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> barkChoice;
        std::cout << std::endl;

    if (barkChoice == 8) goto Menu3;
    while ((barkChoice < 1) || (barkChoice > 8)){
		std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "What does the bark look like?" << std::endl << std::endl;
        std::cout << "1. Smooth" << std::endl;
        std::cout << "2. Scales" << std::endl;
        std::cout << "3. Plates" << std::endl;
        std::cout << "4. Peeling strips" << std::endl;
        std::cout << "5. Shed Ribbon" << std::endl;
        std::cout << "6. Fibrous" << std::endl;
        std::cout << "7. Curved Ridges" << std::endl;
        std::cout << "8. Back" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> barkChoice;
        if (barkChoice == 8) goto Menu3;
    }
        std::cout << leafChoice << veinChoice << edgeChoice << barkChoice << std::endl;

        int treeData = leafChoice * 1000 + veinChoice * 100 + edgeChoice * 10 + barkChoice;

        std::cout << treeData << std::endl;

        std::cout << "Leaf Choice = " << leafChoice << std::endl;
        std::cout << "Vein Choice = " << veinChoice << std::endl;
        std::cout << "Edge Choice = " << edgeChoice << std::endl;
        std::cout << "Bark Choice = " << barkChoice << std::endl;

        char retry;
Menu5:
        std::cout << "Try again? (y/n) ";
        std::cin >> retry;

        if ((retry=='n') || (retry=='N')) return 0;
        else if ((retry=='y') || (retry=='Y')) goto Menu1;
        else {
        std::cout << "Please enter a valid input." << std::endl;
        goto Menu5;
             }
        return treeData;
}


treedata.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef TREEDATA_H
#define TREEDATA_H


class treedata
{
    public:
        treedata();
        virtual ~treedata();
    protected:
    private:
};

#endif // TREEDATA_H
Last edited on
The simple answer is that information is transferred from the function to the caller using it's return value.

In your code, the treeForm() function returns an int. When main() calls treeForm(), it will get an int back. What you do with that int is up to you.

There are other ways functions can manipulate data, but save that till later.

I'm also getting an error when I try to compile telling me that there's multiple definitions of my function treeForm() but it's only listing the one instance of it.

Few things to note in main()

1
2
3
4
5
6
7
8
#include "src/treedata.cpp"

int main()
{
    int treeForm();  // remove this line

    treeForm();  // treeForm() returns an int, where's it going?
}


1) Remove line 5 completely. That line is declaring a function named treeForm(), which can't be done in main or any other function. You've already declared it in datatree.cpp.

2) Line 7 calls the treeForm() function which is great. However, it doesn't do anything with the int that treeForm() returns. You're not require to use the return value, but should either save it in a variable or change treeForm() return type to void instead of int.

3) In Line 1, include the .h header file rather than the .cpp implementation file.
Last edited on
Thanks for the info. I'm still a bit confused on how to assign the return value of the function to another variable. I know this isn't correct but I think it will be easier to say than words:

1
2
3
4
5
6
7
8
9
10
#include "src/treedata.cpp"

int main()
{
    int treeNum; //to assign the data from treeForm()
    treeForm();

    treeForm()=treeNum;

}


How do I do this correctly? I feel like I'm skipping a step but I don't know what it is, errg haha
Ha, I know the feeling. You're moving in the right direction.

The return value from a function can be assigned and used like a variable.

1
2
3
4
int treeNum = 0;
treeNum = treeForm();  // assign to an existing variable

int anotherTreeNum = treeForm();  // combine steps 

If a function returns a value, you probably want to store it in a variable.
Last edited on
I'm going to anticipate the next problem you'll encounter, because it's the same one I had.

Q: The treeForm() function stores four separate ints based on user input. How can the function return all four ints?

A: It can't, at least not directly. Functions may only return one object.

If you're clever with numbers, you could invent a way to combine the variables into a single int, return it, and then interpret the result.

An easier solution is to have treeForm() return an object rather than an int. You can make a custom object that holds all four ints. The object can then be moved between functions as needed.

How are objects made? With structs and classes! I recommend reading up on structs, which let you store basic data fairly easily. http://www.cplusplus.com/doc/tutorial/structures/
Last edited on
Thanks! It seems to be running but it's not returning the correct number, I'm only getting 0 back instead of my test numbers 1234. I'm using this code to turn all 4 questions into one number (thanks to joe):

 
int treeData = leafChoice * 1000 + veinChoice * 100 + edgeChoice * 10 + barkChoice;


I'm thinking that I'll set each tree type to a specific number and pull that data based on the four digit number that was given.

This is what I tried to do:

1
2
3
4
5
6
7
8
int main()
{
    int treeForm();
    int treeData;
    treeData=treeForm();

    std::cout << treeData << " End of program.";
}


After the form ends, treeData is only equal to 0 no matter what I put it for the variables.
Last edited on
Line 129 in your big function up above:

if ((retry == 'n') || (retry == 'N')) return 0;

Perhaps return treeData when the user doesn't want to retry, rather than 0.
oh, geez, yeah, that's probably it haha

thanks again
Topic archived. No new replies allowed.