my baseball program should run but I just can't find the problem why it won't. look program over
This program uses arrays to hold baseball scores for 9 innings. It calculates the high scoring team for each inning and the overall winner of the game.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const int n = 9;
void PrintInput(char[], int[], char[], int[]);
void InningWinner(char[], int[], char[], int[]);
int main()
{
int scores1[n];
int scores2[n];
char team1[n], team2[n];
PrintInput(team1,scores1,team2,scores2);
InningWinner(team1,scores1,team2,scores2);
return 0;
}
void PrintInput(char t1[], int s1[], char t2[], int s2[])
{
cout << "\n********************************************************************\n";
cout << "Team 1: " << t1 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s1[i];
}
cout << "\n";
cout << "Team 2: " << t2 << " ";
for (int i = 0; i < n; i++){
cout << setw(5) << s2[i];
}
}
void InningWinner(char t1[], int s1[], char t2[], int s2[])
{
for (int i = 0; i < n; i++){
if (s1[i] > s2[i])
cout << endl << t1 << " Wins Inning " << i + 1 << endl;
else if (开发者_如何学Pythons2[i] > s1[i])
cout << endl << t2 << " Wins Inning " << i + 1 << endl;
else if (s1[i] == s2[i])
cout << endl << " Inning " << i+1 << " ends in a TIE" << endl;
}
}
All your arrays are used without explicit initialization, which will produced undefined results.
You need to read values into scores1/2 and teams1/2 before you print them or do calculations. You could read from std::cin as in:
std::cout << "Enter " << n << " scores then press enter: ";
int num_scores_read;
for (num_scores_read = 0; std::cin >> scores1[num_scores_read]; ++num_scores_read)
;
if (!std::cin || num_scores_read < n)
{
std::cerr << "error reading score number " << num_scores_read << '\n';
exit(EXIT_FAILURE);
}
(similar for scores2 etc.)
OR, you could read them from a file (similar to above, but use
#include <fstream>
std::ifstream file(filename);
...as above but use "file" in place of "std::cin"...
OR, just hard code some sample values in your program to get you started:
int scores1[n] = { 1, 3, 5, 1, 3, 5, 4, 5, 3 };
精彩评论