Hello

I am trying to write a multiplication table into a function but am not getting the result i want ive been looking at it for about 3 hours now and cant figure what is going on im not getting errors it runs fine but isnt givig me a neat table the extra numbers after each line i cant understand if someone could help either by adjusting or by talking me through the logic i would appriciate it below is my code for the project

thanks :)

``````void Multitablefunc(int intMultDisplay)//multiplication table generator function
{
for( int i=1; i<=intMultDisplay; i++ )
{
cout<<setw(5)<<i<< endl;
for( int j = 1; j<=intMultDisplay ; j++ )
{
cout << setw(5)  << i*j ;

}
}
}
``````

There is indeed something wrong with your logic. I've fixed your code and added comments that show what the code is supposed to do. If anything is unclear after looking at it let me know:

``````#include <iostream>
#include <iomanip>

using namespace std;

// Note: If you don't know what 'const' is, just remove it or ignore it. It's just a habit of mine but isn't essential to the solution.
void Multitablefunc(const int n)
{
// The first line that is to be printed shows the heading for the columns.
// The first column is reserved because it will contains the row headers. So skip over that.
cout << setw(5) << " ";

// Then print the remaining numbers. This will simply be 1 till n.
for (int column = 1; column <= n; column++)
{
cout << setw(5) << column;
}
// We're done with the headers, go to the new line.
cout << endl;

// We can now print the rest of the table.
for(int row = 1; row <= n; row++)
{
// Every row starts with the row header, which is the number that is multiplied with.
cout << setw(5) << row;

// Then print the multiplication values, starting from the lowest.
for(int column = 1; column <= n ; column++)
{
cout << setw(5) << row*column;
}

// We're done with this row go to the next line before printing the next row.
cout << endl;
}
}

int main()
{
Multitablefunc(5);
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.