Basic integer multiplication

Hi there,
Im having some trouble writing a basic program in C++ to multiply two integers, the problem is with the formatting that I cant get right....

The necessary format would be:
99
* 999
-----
98901
(All of the numbers above should be aligned to the right, except the multiplication sign which should remain on the left side)

This is just what I need it to show on the end screen.


Remember, I am a beginner, so here's what I have right now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
	int a = 99, b = 999, sum = a * b;
	
	cout <<setw(5);
	cout << a <<endl;
	
	cout <<setw(5);
	cout << b <<endl;
	
	cout <<setw(5) <<setfill('-') <<endl;
	
	cout <<setw(5);
	cout << sum <<endl;
	
return 0;
}


Im sure im putting the setw and setfill in the wrong places. But im just really struggling with the formatting aspect of C++.

Any help would be appreciated!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>

int main () {

	const int a = 99  ;
	const int b = 999 ;
	const int product = a * b;

	const int width = 5 ;

	std::cout << ' ' << std::setw(width) << a << '\n'
	          << '*' << std::setw(width) << b << '\n'
	          << std::setw(width+2) << std::setfill('-') << '\n'
	          << ' ' << std::setw(width) << product << '\n' ;
}

http://coliru.stacked-crooked.com/a/12847f7336ef855f
Topic archived. No new replies allowed.