Baseball program question

What is an easy way to display the score after each inning? Instead of adding it all up at the end I want it to say Home Team is winning 8-2 after the first, second, third, etc inning. Stuck on this. I know how to display the added up value of all the elements AFTER the 9 innings are complete but how would I display it after every single inning.


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
#include <iostream>
#include <string>

using namespace std;



int main()
{
	int homeTeam[9];
	int awayTeam[9];
	int awayRuns;
	int homeRuns;
	int homeSum = 0;
	int awaySum = 0;
	

	for (homeRuns = 0; homeRuns < 9; homeRuns++)
	for	(awayRuns = 0; awayRuns < 9; awayRuns++)
	{
		cout << "Please enter the score after each inning for the Home Team: \n";
		cin >> homeTeam[9];
		cout << "Please enter the score after each inning for the Away Team: \n";
		cin >> awayTeam[9];
		homeSum = homeSum + homeTeam[9];
		awaySum = awaySum + awayTeam[9];
		cout << endl;
		
	}
	
	
	

	system("pause");
	return 0;
}

If an array is declared like so: int arr[9];, then it has a size of 9, and its valid indices range from 0 to 8, inclusive. In other words, actually accessing arr[9] goes out of bounds; you can only access arr[0] to arr[8].

Perhaps something like this:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int homeTeam[9];
	int awayTeam[9];
	int homeSum = 0;
	int awaySum = 0;
	
	for (int i = 0; i < 9; i++)
	{
		cout<< "Please enter the Home Team's score for Inning " << i + 1 << ": ";
		cin >> homeTeam[i];
		cout<< "Please enter the Away Team's score for Inning " << i + 1 << ": ";
		cin >> awayTeam[i];
		
		homeSum += homeTeam[i];
		awaySum += awayTeam[i];
		
		std::string status;
		if (homeSum < awaySum)
		{
			status = "losing";
		}
		else if (homeSum == awaySum)
		{
			status = "tied";
		}
		else 
		{
			status = "winning";
		}
		
		std::cout << "Home Team is " << status << " " << homeSum << "-" << awaySum << '\n';
		cout << endl;
	}

	return 0;
}
Last edited on
thanks Ganado! Makes sense!
Topic archived. No new replies allowed.