Help with fixing this function?

Im just trying to get the product of two numbers but receive "2752404" as an answer for anything entered.
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
#include <iostream>

using namespace std;

int timesN (int, int);

int main()
{

int n1, n2, product;

    cout <<"Enter the first number you want to multiply"<< endl;
    cin>> n1;
    cout <<"Enter the second number you want to multiply"<< endl;
    cin>> n2;

    int timesN(int, int);

    cout<< n1 << " times " << n2 << " is " << product << endl;

    return 0;
}
int timesN (int number, int N)
{
     return (number * N);

}

when i set product equal to timesN i get an error saying there cant be a primary expression before int?
Last edited on
Line 17 doesn't do anything at all, you simply declare that the function exists.

You never assign any value to the variable product, so it just holds a random garbage value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int timesN (int, int);
int main()
{
int n1, n2;
    cout <<"Enter the first number you want to multiply"<< endl;
    cin>> n1;
    cout <<"Enter the second number you want to multiply"<< endl;
    cin>> n2;
    cout<< n1 << " times " << n2 << " is " << timesN(n1,n2) << endl;
    return 0;
}
int timesN (int n1, int n2)
{
     return (n1 * n2);
}


There, fixed it for you. In your code, in line 25 you try to multiply "number" by "N", but you haven't defined those. Instead you defined "n1" and "n2", so why not use those? Also the integer "product" is redundant, you can just give those values to the "timesN" function directly and get the answer that way.
Last edited on
Topic archived. No new replies allowed.