Spacing math problem

I am trying to make a simple match program for my kids. I want to make it look as aesthetically pleasing as possible and I can not get the number to line up properly. I am a new c++ student so this question may be rather dumb but how do I get the output to line up so that the answer is vertical with the numbers in the problem?

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>
#include <iomanip>
#include <time.h>
#include <string>
#include <cstdlib>
#define over2  "\t\t"
#define over3  "\t\t\t"
#define over4  "\t\t\t\t"
#define down5  "\n\n\n\n\n"
#define down8  "\n\n\n\n\n\n\n\n"
#define down10 "\n\n\n\n\n\n\n\n\n\n"
#define down12 "\n\n\n\n\n\n\n\n\n\n\n\n"

using namespace std;

void add(int,int,string,char);
//void sub();
//void multi();
//void div();

int main(int argc, char** argv) 


{
	int num1, num2;
	
	unsigned seed = time(0);
	srand(seed);
	num1 = 1 +rand() % 4;	
	num2 = 1 +rand() % 4;	
	
	
	cout << "This program is a tool for young kids to learn basic math.\n";
	cout << "Just press any key and the learning will commence!\n";
		 
	system("CLS");
	
	add(num1, num2, "Addition", '+');
	cout << over3 << num1+num2 << endl;
	
	cout << down8;	
	return 0;
}

void add(int num1, int num2, string operation, char symbol)
{
	cout << down8;
	cout << over3 << operation << endl;
	cout << over3 << "________\n\n";
	cout << over3 << ' ' << setw(3) << num1 << endl;
	cout << over3 << symbol << setw(3) << num2 << endl;
	cout << over3 << "________\n\n";

		 
}


closed account (o3hC5Di1)
Hi there,

I suggest you have a look at these two pages:

http://www.cplusplus.com/reference/iomanip/setw/
http://www.cplusplus.com/reference/ios/right/

Please do let us know if you require any further help.

All the best,
NwN
ASIDE

As you're already using std::string, rather than define down5, down8, etc you could use std::string's fill constructor (string(size_t n, char c);) like this

1
2
3
4
inline string down(size_t N)
{
	return string(N, '\n');
}


and similarly for over, then use them like

1
2
3
4
5
6
7
8
9
void add(int num1, int num2, string operation, char symbol)
{
	cout << down(8);
	cout << over(3) << operation << endl;
	cout << over(3) << "________\n\n";
	cout << over(3) << ' ' << setw(3) << num1 << endl;
	cout << over(3) << symbol << setw(3) << num2 << endl;
	cout << over(3) << "________\n\n";
}


Andy
Last edited on
Thanks for the help. I am just starting c++ in college so I am not sure about what an std::string's fill constructor is but thank your for the reply!
closed account (o3hC5Di1)
No worries - you will learn all about constructors when your c++ course addresses classes. If you are interested in the meanwhile:

http://www.cplusplus.com/doc/tutorial/classes/

All the best,
NwN
I am not sure about what an std::string's fill constructor

That's where the documentation on this site (and elsewhere) comes into play.

http://www.cplusplus.com/reference/string/string/string/

(6) fill constructor
    Fills the string with n consecutive copies of character c.

Andy
Topic archived. No new replies allowed.