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.
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:
#include <iostream> //Practicing on Functions!!
#include <iomanip>
usingnamespace 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 onint 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.