0

string lenght

I made a program with arrays c++ to make a table of player names and score then print it in the form of table on the left print names and on right there scores but when i put the names too long the score is dis-aligned according to the string size.kindly show me a way to print the whole table in aligned format. https://code.sololearn.com/cwYeJCTx34Kw/?ref=app

14th Feb 2021, 8:29 AM
Shaban Kiyani
Shaban Kiyani - avatar
5 Answers
+ 1
There is no ideal or "correct" way to do this. Your best bet would be to crop the <name> string up to a certain length, when its length exceeds the limit. Actually, even this may not completely solve the problem because each character differs in width (except you're sure monospace font is used)
17th Feb 2021, 12:45 AM
Ipang
+ 2
Can you share a link to your code in thread's Description? it may help others to see the problem and assist you through appropriately. https://www.sololearn.com/post/75089/?ref=app
14th Feb 2021, 8:36 AM
Ipang
+ 1
i have edit it with my code Note:I didn't used sololearn to run this code i used dev c++ on pc. link:https://code.sololearn.com/cwYeJCTx34Kw/?ref=app
14th Feb 2021, 8:50 AM
Shaban Kiyani
Shaban Kiyani - avatar
+ 1
So what is the correct way to write this code for proper alignment and other errors and also you said use inputting both values of string and int in one loop body but i want to do it separately becoz every time my loop runs it will ask for a name then its score but i wanna give name input then score input.I cannot initialize loop with 0 coz i entered player1,player2....and so on for this output and input i initialized loop from 1.
16th Feb 2021, 2:45 PM
Shaban Kiyani
Shaban Kiyani - avatar
0
Your loops counter initialize by 1, you need to change them to initialize from 0. for( <var> = 0; <var> < <limit>; <var>++ ) You probably can mix reading input for names and score in one for-loop, like you do when printing names and scores. About names alignment, maybe you can crop the name when their size exceeds a certain value. You can use conditionals inside the loop for printing outputs Pseudo: if length of string n-th greater than <x> print <x> character of string n-th else print string n-th print score n-th However, difference of width of each character (except for monospace fonts), may still cause inconsistency in output alignment.
14th Feb 2021, 11:54 AM
Ipang