How to pass a value from one function to another.

So I am currently enrolled in a coding class and as part of our homework we had to write a code that will make a pyramid out of $. We are working with functions right now so we have to do it by using two functions. One is the int getNumberOfRowsFromUser () and the second is void drawPyramid (int). I think my code will work but I cannot seem to get the value that the user inputs to go into my drawPyramid function. I know it is probably a very simple fix and I have been trying to figure it out for an hour or two now and can not. Can somebody please help me.

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
#include <iostream>
#include <string>
using namespace std;

int getNumberOfRowsFromUser(int)
{
    int num;
    cout<< "Enter how many rows you would like your pyramid to be." << endl; 
    cin >> num;
    return num;
}

void drawPyramid(int num)
{
    for(int i = 1, k = 0; i <= num; ++i, k = 0)
    {
        for(int space = 1; space <= num-i; ++space)
        {
            cout <<"  ";
        }
        
        while(k != 2*i-1)
        {
            cout << "$ ";
            ++k;
        }
        cout << endl;
    }
}

int main()
{
    int num;
    getNumberOfRowsFromUser (num);
    drawPyramid(num);
    
return 0;
}
getNumberOfRowsFromUser is returning an int, but you are not picking it up when you call

getNumberOfRowsFromUser (num);

so if you do

num = getNumberOfRowsFromUser (num);

then the thing you pass to dawPyramid, is actually the int returned by the other function.

you can even do

dawPyramid(getNumberOfRowsFromUser(num))

so removing the need for the "middle man"
Last edited on
And yeah there we go really easy solution it was. Thank you.
Topic archived. No new replies allowed.