Console output formatting.

Hello I've written a little console game and I'm wondering how I get the output to stay at a set position, as you will se from the output, the output is slightly shifting to the left as the game goes on, I want the text that is after the ||||| signs to be at a fixed position and should not be depending on wether how many || that is printed.

You can see the output here: http://ideone.com/Dxn9wA

Any ideas how I can fix it?

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <cstdio>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
 
 
int printSticks(int sticks);
int getMove(int sticks);	
int computerMove(int sticks);
 
/*****************************************************************
ANROP: playGame();
VERSION: xxxxx xx
UPPGIFT: Låter användaren spela ett parti pinne mot datorn
******************************************************************/
void playGame()
{
int numSticks = 15 + (rand() % 11); // slump 15..25
while (numSticks > 0)
{
printSticks(numSticks);
 
cout << "\tHur många vill du plocka? ";
int userMove = getMove(numSticks);
numSticks -= userMove;
 
if(numSticks <= 0)
{
cout << "Du vann, turgubbe!" << endl;
break;
}
 
cout << "\nResultat:\t\t";
for (int i = 0; i < numSticks; i++) // skriver ut resultat i pinnar
{	
if (i >= 0 && ((i % 5) == 0))
cout << " |";
else
cout << "|";
}
 
int myMove = computerMove(numSticks);
 
cout << "\tDatorn tar\t" << myMove << endl;
numSticks -= myMove;
 
if (numSticks <= 0)
{
cout << "Datorn vann, looooser!!" << endl;
break;
}
}
}// playGame
 
/*****************************************************************
ANROP: int num = getMove(sticks);
UPPGIFT: Läser in användarens drag (iterativt tills ett giltigt drag anges)
INPAR: sticks = antalet stickor i den aktuella högen
RETUR: num = användares drag, 1 eller, 2 men <= sticks
ANM: vid anrop måste sticks > 0, ty annars finns inget giltigt drag
******************************************************************/
int getMove(int sticks)
{
if (sticks > 0)
{
int plocka = 0;
cin >> plocka;
 
while (plocka != 1 || plocka != 2)
{
if (plocka == 1 && sticks > 0)
{
return 1;
}
else if (plocka == 2 && sticks > 1)
{
return 2;
}
else
{
cout << "Gick ej att plocka " << plocka << "" << " st stickor, välj igen: ";
cin >> plocka;
}
}
}
else
{
return 0;
}
return 0;
}//getMove
 
/*****************************************************************
ANROP: int num = computerMove(sticks);
UPPGIFT: Returnerar datorns drag
INPAR: sticks = Antalet stickor i den aktuella högen
RETUR: num = datorns drag, 1 eller 2 men <= sticks
ANM: vid anrop måste sticks > 0, ty annars finns inget giltigt drag
******************************************************************/
int computerMove(int sticks)
{
if (sticks > 0)
{
int plocka = 0;
plocka = rand() % 3;
 
while (plocka != 1 || plocka != 2)
{
if (plocka == 1 && sticks >= 0)
{
return 1;
}
else if (plocka == 2 && sticks >= 1)
{
return 2;
}
else
{
plocka = rand() % 3;
}
}
}
else
{
return 0;
}
return 0;
}// computerMove()
 
/*****************************************************************
ANROP: printSticks(sticks);
UPPGIFT: Skriver ut sticks stycken stickor gruperade i
grupper av fem, föjt av ett antal
mellanslag så att total 30 tecken skrivs ut
******************************************************************/
int printSticks(int sticks)
{
cout << "\nAktuell hög:\t ";
 
for (int i = 0; i < sticks; i++)
{	
if ((i % 5) == 4)
cout << "| ";
else
cout << "|";
}
 
 
return 0;
}
 
int main()
{ setlocale( LC_ALL, "swedish");
 
while (true)
{
playGame();
cout << "Vill du spela mera?";
string answ;
cin >> answ;
if (answ[0]!='j' && answ[0]!='J')
{
cout << "Fegis!!" << endl;
break;
}
}
return 0;
}// main 
Last edited on
You can set the width of each output by using std::setw (#include <iomanip>).

This will output "Aktuell hög:" padded with space so that the output is exactly 16 characters. This is useful because we know that the next output will start at position 16. std::left is used to make the text left aligned.
 
cout << '\n' << setw(16) << left << "Aktuell hög:";


To output the sticks with std::setw you will have to output all the sticks at the same time. You can easily do this by adding the sticks to a std::string variable that you output at the end.
1
2
3
4
5
6
7
8
9
std::string str;
for (int i = 0; i < sticks; i++)
{	
	if ((i % 5) == 4)
		str += "| ";
	else
		str += "|";
}
cout << setw(30) << left << str;


You don't need to use std::setw when outputting the question unless you want the user input to line up as well.

Now do the same with the "Resultat" row. You don't have to use the same values with setw that I did but make sure you use the same values for both rows, except you might have to use a value that is one larger when outputting "Aktuell hög:" because 'ö' is actually stored and counted as two characters (bytes).
Thanks, works like a charm ;)
Topic archived. No new replies allowed.