Hello hekanayake,
Based on above i assign values to two arrays .
cin>>a[2][x]=P>>a[3][y]=Q;
|
Where is the other array?
First start with fixing the code so it is easier to read:
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
|
#include <iostream>
#include <limits>
int main()
{
constexpr int MAXROWS{ 4 }, MAXCOLS{ 12 };
//constexpr int MAXSCORE{ ? }; // <--- Replace ? with highest possible scroe.
int teamScores[MAXROWS][MAXCOLS]{}; /*array to save data */
int numOfPeriods{};
do
{
std::cout << "Enter number of periods: ";
std::cin >> numOfPeriods;
if (!std::cin || numOfPeriods > MAXCOLS)
{
if (!std::cin)
{
std::cout << "\n You must enter a number!\n\n";
std::cin.clear(); // <--- Resets the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Clears the input buffer.
}
else if (numOfPeriods > MAXCOLS)
{
std::cout << "\n To many periods! Max is " << MAXCOLS << ".\n\n";
}
}
} while (numOfPeriods > MAXCOLS || numOfPeriods == 0);
std::cout << "\nEnter scores of team A\n";
for (int col = 0, row{}; col < numOfPeriods; col++)
{
std::cout << " Enter score #" << col + 1 << ": ";
std::cin >> teamScores[row][col];
}
//for (int x = 0; x < numOfPeriods; x++) // <--- Good for testing, but not necessary otherwise.
// std::cout << teamScores[0][x] << ' ';
//std::cout << std::endl;
std::cout << "\nEnter scores of team B\n";
for (int col = 0, row{ 1 }; col < numOfPeriods; col++)
{
std::cout << " Enter score #" << col + 1 << ": ";
std::cin >> teamScores[row][col];
}
//for (int y = 0; y < numOfPeriods; y++) // <--- Good for testing, but not necessary otherwise.
// std::cout << teamScores[1][y] << std::endl;
int P{ 4 }; // <--- Should it be constexpr int P{ 4 }; ? Do you intend to have these variables change?
int Q = 2;
int R = 3;
//for (int col = 0/*, y = 0*/; col < numOfPeriods; col++/*, y++*/)
// if (teamScores[0][col] > teamScores[1][y])
// {
// std::cin >> teamScores[2][col] = P >> teamScores[3][col] = Q;
// }
// else if (teamScores[0][col] == teamScores[1][col])
// {
// std::cin >> teamScores[2][col] = R >> teamScores[3][col] = R;
// }
// else
// {
// std::cin >> teamScores[2][col] = Q >> teamScores[3][col] = P;
// }
return 0; // <--- Not required, but makes teamScores good break point.
}
|
The code shows some concepts that you should fine useful.
A better use of blank lines along with a good variable name makes the code much easier to follow.
I removed the extra blank lines that are not needed and added some that are.
"main" starts with the constant variables. I find this makes it easier should they ever need to be changed.
Next it is always a good idea to initialize the variables. The empty {}s, the uniform initializer available from C++11 on, make this very easy even for the array.
The do/while loop allows for a valid entry and the if statement checks for anything that is incorrect.
The formatted input
std::cin >> numOfPeriods;
is expecting a number to be entered. If not "cin" will fail and will become unusable the rest of the program.
The do/while loop keeps asking until you enter a valid number. This is done to keep from going past the end of the array and to catch any problem with "cin".
From line 34 on try to use the new line (\n) instead of the "std::endl". You may not see it in a program this small, but in a large program the overhead of "endl" can cause the program to run slower.
The other thing you may not realize is:
1 2
|
std::cout << "Enter number of periods: ";
std::cin >> numOfPeriods;
|
There are 2 things here. First leaving out any (\n) or "endl" will cause the "cin" to be on the same line as the prompt. Tat is what the ": " is for. And second a "cout" followed by a "cin" the "cin" will flush the output buffer before it takes any input. At least as I understand it.
The section that is commented out is because, as you have been told, does not work and I do not have any idea what you want to do.
The code like
std::cin >> teamScores[2][col] = P >> teamScores[3][col] = Q;
you will need to explain what your intention is and what you want it to do because only you know what you need.
Andy