Problem with Setw function...

Please note the only print_strip(int picked_team, int wickets) function in the program.
The problem I am getting is in the setw() command I am using and it isn't giving the desired result.

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
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int scores[11] = { 0 }, balls[11] = { 0 }, extras;
void input(string [][12], int); 
int sum(int[]); 
int pickteam();
int opponent_team; 
extern string team[5][12]; 
void print_scoreboard(int);
void print_strip(int, int); 
string get_short_name(int); 
int main()
{
	int picked_team = pickteam(); 
	input(team, picked_team);
	system("cls");
	print_scoreboard(picked_team); 
	return 0;
}
int pickteam()
{
	cout << endl;
	cout << "Pick your Team" << endl << endl
		<< "1) Pakistan" << endl
		<< "2) Australia" << endl
		<< "3) England" << endl
		<< "4) India" << endl
		<< "5) New Zealand" << endl << endl
		<< "Your choice: "; 
	int choice; 
	cin >> choice;
	while (choice < 1 || choice > 5)
	{
		cout << "Invalid pick. Please enter again: "; 
		cin >> choice; 
	}
	system("cls");
	cout << endl;
	cout << "Pick Opponent Team" << endl << endl
		<< "1) Pakistan" << endl
		<< "2) Australia" << endl
		<< "3) England" << endl
		<< "4) India" << endl
		<< "5) New Zealand" << endl << endl
		<< "Your choice: "; 
	cin >> opponent_team;
	while (opponent_team < 1 || opponent_team > 5)
	{
		cout << "Invalid pick. Please enter again: "; 
		cin >> opponent_team; 
	}
	return (choice - 1);
}
void input(string team[][12], int picked_team)
{
	system("cls");
	cout << endl << endl;
	for (int i = 0; i < 11; i++)
	{
		print_strip(picked_team, i);
		cout << endl << endl; 
		cout << team[picked_team][i] << " is batting.\nEnter his score: "; 
		cin >> scores[i]; 
		while (cin.fail())
		{
			cout << "Invalid score, please enter again: "; 
			cin.clear(); 
			cin.ignore(); 
			cin >> scores[i]; 
		}
		cout << "Enter the Balls faced by " << team[picked_team][i] << ": "; 
		cin >> balls[i];
		while (cin.fail())
		{
			cout << "Invalid balls, please enter again: ";
			cin.clear();
			cin.ignore();
			cin >> balls[i];
		} 
	}
	cout << "Extras: "; 
	cin >> extras; 
	while (cin.fail())
	{
		cout << "Invalid score, please enter again: ";
		cin.clear();
		cin.ignore();
		cin >> extras;
	}
}
void print_strip(int picked_team, int wickets)
{
	for (int i = 1; i <= 90; i++)
		cout << "-";
	cout << endl; 
	cout << left << setw(10) << get_short_name(picked_team) << " vs " << get_short_name(opponent_team);
	cout << left << setw(40) << team[picked_team][wickets] << " 0*";
	cout <<	 right << setw(40) << "Overs: " << (int)(sum(balls) / 6) << "." << (int)(sum(balls) % 6) << endl; 
	cout << left << setw(10) << get_short_name(picked_team) << " " << (int)(sum(scores) + extras) << "-" << wickets; 
	cout << left << setw(40); 
	if (wickets == 0)
		cout << team[picked_team][wickets + 1] << " 0";
	else
		cout << team[picked_team][wickets - 1] << " " << scores[wickets - 1]; 
	srand(time(0));
	int bowler = rand() % 5 + 7;
	cout << right << setw(40) << "Bowler: " << team[opponent_team][bowler] << endl; 
	for (int i = 1; i <= 90; i++)
		cout << "-";
	cout << endl; 
}
string get_short_name(int team)
{
	string Pakistan = "PAK", India = "IND", Australia = "AUS", New_Zealand = "NZD", England = "ENG"; 
	switch (team)
	{
	case 0:
		return Pakistan;
	case 1:
		return Australia; 
	case 2:
		return England; 
	case 3:
		return India;
	case 4:
		return New_Zealand; 
	}
}
int sum(int values[])
{
	int res = 0; 
	for (int i = 0; i < 11; i++)
	{
		if (values[i] != 0)
			res = res + values[i]; 
	}
	return res; 
}
void print_scoreboard(int picked_team)
{
	cout << endl << " " << left << setw(45) << team[picked_team][11] << left << setw(15) << "Runs" << left << setw(15) << "Balls" << endl << endl;
	for (int i = 0; i < 11; i++)
	{
		cout << " " << left << setw(45) << team[picked_team][i] << left << setw(15) << scores[i] << left << setw(15) << balls[i] << endl << endl;
	}
	cout << right << setw(45) << "Extras         " << " " << left << setw(15) << extras << endl << endl;
	cout << "   Overs: " << (int)(sum(balls) / 6) << "." << (int)(sum(balls) % 6)
		<< right << setw(30) << "Total: " << (int)(sum(scores) + extras)
		<< right << setw(20) << "Run Rate: " << setprecision(2) << (int)(sum(scores) + extras) / (float)(sum(balls) / 6);

	cout << endl << endl << "Press Enter to continue..."; 
	getchar(); 
	getchar(); 
}
string team[5][12] = { {"Imam ul Haq", "Fakhar Zaman", "Babar Azam", "Muhammad Hafeez", "Haris Sohail", "Sarfraz Ahmed", "Imad Wasim", "Shadab Khan", "Wahab Riaz", "Muhammad Amir", "Shaheen Afridi", "Pakistan" },
	{"Aaron Finch", "David Warner", "Usman Khawaja", "Glenn Maxwell", "MP Stoinis", "AT Carey", "Pat Cummins", "Mitchell Starc", "Nathan Lyon", "JP Behrendorf", "Australia"},
	{"Jame Vince", "JM Baistow", "Joe Root", "Eion Morgan", "Ben Stokes", "Joe Buttler", "CR Woakes", "Moeen Ali", "Adil Rashid", "JC Archer", "MA Wood", "England"},
	{"Rohit Sharma", "Shikhar Dhawan", "Virat Kohli", "K. L. Rahul", "Vijay Shankar", "MS Dhoni", "Kedar Jadhav", "Hardik Pandya", "Muhammad Shami", "Jasprit Bumrah", "Bhuveneshwar Kumar", "India"},
	{"Martin Guptill", "Colin Munro", "Kane Williamson", "Ross Taylor", "Tom Latham", "James Neesham", "Colin de Grandhomme", "Mitchell Santner", "Matt Henry", "Lockie Ferguson", "Trent Boult", "New Zealand"} };



The function print_strip is giving this output... I don't want this.
What I wanted is this that "AUS vs IND" would be set to setw(10) (however, here, it is only setting "AUS" to setw(10).
Similarly, I want "Aaron Finch 0*" to be set to setw(40) but it is only setting "Aaron Finch" to setw(40).
How can I fix this issue? Thank you!

1
2
3
4
5
6
7
AUS        vs INDAaron Finch                              0*                                 Overs: 0.0
AUS        0-0David Warner                             0                                Bowler: Bhuveneshwar Kumar
------------------------------------------------------------------------------------------


Aaron Finch is batting.
Enter his score:
For the first problem, just get rid of the setw(10). The strings already (and always) add up to 10. There's no need for padding.

For the second problem, you can append the "0*" to the name and print the resulting string:

 
    cout << left << setw(40) << team[picked_team][wickets] + " 0*";

Ok thank you, this pretty much solves the issue..
But it still is causing problems in the second line,

 
cout << left << setw(10) << get_short_name(picked_team) + " " << (int)(sum(scores) + extras) + "-" + wickets;


I tried to combine all these with '+' but since there are some integers, I think it isn't working. And it isn't even showing "wickets" here now (since it's an integer value I reckon)

Previously, I had it coded like this,

 
cout << left << setw(10) << get_short_name(picked_team) << " " << (int)(sum(scores) + extras) << "-" << wickets;


Now, the thing is, the score will change and it could go from single digit to double to triple and maybe even to 4 figures... Similarly for wickets, they can be of single digit or of double digit too...

So, how would I solve this issue here? Thank you!
And also thanks for replying previously

Edit: This is how my output is looking like now, still lots of issues to solve :/
1
2
3
4
5
6
7
8
PAK vs ENG   Imam ul Haq 0*                                                        Overs: 0.0
PAK        0-0Fakhar Zaman 0                                                          Bowler: Moeen Ali
------------------------------------------------------------------------------------------


Imam ul Haq is batting.
Enter his score:
Last edited on
Remember, setw only applies to the thing after the next << so as soon as you use another << that's it. Only the previous stuff will be padded into the requested field length.

To fit all of that into a single setw field length you need to make it all one string. You can change integers into strings with to_string and then you can use + to append those strings together:

1
2
3
4
5
6
    cout << left << setw(10)
         << get_short_name(picked_team)
            + " "
            + to_string((int)(sum(scores) + extras))
            + "-"
            + to_string(wickets);

However, you might want to do it like this instead:

1
2
3
4
5
6
    cout << get_short_name(picked_team)
         << ' '
         << right << setw(2) << (int)(sum(scores) + extras)
         << '-'
         << setw(2) << wickets
         << ' ';

Last edited on
Ok, I got it :)
I went with to_string option and it worked perfectly for me (however, pretty sure the latter would have been great too but it still kind of bugged me as it might have led to 1 or 2 spaces difference with every input)

Thanks for the beautiful explanation!
Topic archived. No new replies allowed.