Apparently this, and some other combinations, make the text appear like that:
While it should appear like that:
cout << left << setw(20) << strc.name1 << ' ' << strc.name2; cout << right << strc.total4 << endl;
Tried many combinations, seems like I couldn't hit the jackpot. Couldn't find any references focused on a situation like this (separation).
An alternative to this would be printing whitespaces from an array (can't use strings).
It is doing exactly what you've told it to do.
The `setw' mechanic does not stick; it is limited to the next insertion.
The correct way to do this is with counting. You count the length of both outputs so that you can figure out how many characters to fill (via `setw') and optionally correct for lengths that extend beyond the relevant column.
It is easier however to write those two bits of output to a temporary string stream and write the result at once.
How can I do that? Or perhaps there is another way to make it work like it should? Note that I must use arrays.
I can't use strings because I must use arrays. I'm getting ready for an exam, and we've this dumb system around here...
If you know enough about using character arrays to have written the code that manipulates `strc.name1' you know enough to do the counting I've suggested.
If you didn't write that code, I suggest that you are in deep trouble.
I'll be happy to help, but I'm not going to do it for you. I don't think anyone else here will do it for you.
Try to do what I've suggested after a fashion. For example, do you know how to get the lengths of an input character array?
Well I've made it work now, but I think that there is an easier way.
I know only one way - put a symbol that means "the array ends HERE," I'm not aware of a convenient way that strings offer to know the length of an array.
Here is my for loop that does it, which I think is overly complicated. As, often, most of my code:
if(strc.name1[o] == '0') strc.dummy++;
If you don't know how "C strings" (which are simply character arrays following a specific convention) work and `strc.name1' is an array you didn't write the code manipulating it correctly.
Do you want to revise any of your statements?
Or do you want to post the other bits of your code?
By the way, your counting illustration is wrong; if that code is supposed to show how to count until a terminator is found your condition is wrong.
My code works and I'm looking to improve my coding skills regarding this area, as the time is limited during exam. Overly-complicated code can take too long, so I'd rather post some code for revision instead.
As for the counting illustration, I fill the array with memset(strc.name1,'0',20); to "clear" it, so basically looks for any fields that aren't "cleared."
Here's some of my code regarding reading information from file:
I think that's a correct way to do it, as whitespaces can't (or shouldn't) be read, when using arrays.
// I read the name and the surname to two arrays
// Since they are in the same line, separated by a whitespace
ifile >> strc.name1;
ifile >> strc.name2;