Function Declaration Confusion

I have an exercise that instructs me to create a program that outputs a times table up to twelve of any number. {e.g.: 2*1, 2*2......2*12} And the program has to have at least three functions. The confusion comes in the add function where I am expecting the number to output 2*1, 2*2, 2*3.....BUT instead it outputs 2+2=4, 4+4=8, 8+8=16, etc. The problem is at the declaration of t in the add function. Can someone please lend me a hint.

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
#include <iostream>  //Practicing on Functions!!
#include <iomanip>
using namespace std;

int add(int& num);
void multiply(double num);
void getInput(int& num);
int originalNumber(int num);

int main()
{
    int newNum, theUsersNumber, counter = 0;

    cout << "Enter a number: ";
    getInput(theUsersNumber);

    cout << originalNumber(theUsersNumber) << " ";
    newNum = theUsersNumber;

    while(counter <= 11)
    {
    add(newNum);
    counter++;
    }

    return 0;
}
//End Main Function



int add(int& num)
{
    int t;

    t = originalNumber(num); //Shouldn't t always keep the same value
    num = t + num;
    cout << num <<  " ";  
}
//End Add Function



void getInput(int& num)
{
    cin >> num;
}
//end getInput function



int originalNumber(int num)
{
    return num;

}
//end originalNumber function 


What keeps happening - If input = 2

Output: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192


Expecting - If input = 2

Output: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26



I'd figure if the input was 2, t would always be 2 but for some reason, it changes.



Last edited on
I'd suggest using these three functions:
1
2
3
int GetNumber();
void DisplayTimesTable(int num);
void DisplayMultiplication(int num, int multiplier);


GetNumber should ask for the user to enter a number and return that number to a variable.
DisplayTimesTable should use a for loop, 1-12, and call DisplayMultiplication with each value.
DisplayMultiplication should display the num * multiplier on the screen.
Thanks Volatile Pulse. The code you specified works like a charm. Using a counter variable in a loop made things much simpler but I am still wondering what went wrong in the last program I wrote with the t variable on line 36. Could you shed a little light on why it might not be working. Here is the code you wrote:


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
#include <iostream>  //Practicing on Functions!!
#include <iomanip>
using namespace std;

int getNumber();
void displayTimesTable(int num);
void displayMultiplication(int num, int multiplier);


int main()
{   int userNumber;

    userNumber = getNumber();       //gets number

    displayTimesTable(userNumber);  //

    return 0;
}
//End Main Function



int getNumber()
{
    int num;

    cout << "Enter a number: ";
    cin >> num;
    return num;
}
//end getNumber function




void displayTimesTable(int num)
{
    for(int counter = 1; counter <= 12; counter++)
    {
    displayMultiplication(num, counter);
    }
}
//end displayTimesTable function




void displayMultiplication(int num, int multiplier)
{
    cout << num * multiplier << " ";
}
//end displayMultiplication function 


Input: 2

Output: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24


Last edited on
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
#include <iostream>  //Practicing on Functions!!
#include <iomanip>
using namespace std;

int add(int& num);
void multiply(double num);
void getInput(int& num);
int originalNumber(int num);

int main()
{
    int newNum, theUsersNumber, counter = 0;

    cout << "Enter a number: ";
    getInput(theUsersNumber);

    cout << originalNumber(theUsersNumber) << " ";
    newNum = theUsersNumber;

    while(counter <= 11)
    {
    add(newNum);
    counter++;
    }

    return 0;
}
//End Main Function


// You're calling add by reference this will modify the local variable in the main function.
// the first call would be 2 in your example, so then after 2 + 2, it equals 4
// the second call would be 4, so then 4 + 4 = 8
// third call num = 8, 8 + 8 = 16
// and so on
int add(int& num)
{
    int t;

    t = originalNumber(num); //Shouldn't t always keep the same value
    num = t + num;
    cout << num <<  " ";  
}
//End Add Function



void getInput(int& num)
{
    cin >> num;
}
//end getInput function



int originalNumber(int num)
{
    return num;

}
//end originalNumber function  


Check out the add function. It's not doing quite what you expected.
Alright much thanks
Topic archived. No new replies allowed.