### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` `````` #include #include #include #include #include #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"; }``````

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

 ``1234`` ``````inline string down(size_t N) { return string(N, '\n'); }``````

and similarly for over, then use them like

 ``123456789`` ``````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!
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.