Hello Atharva k12,
Thank you for using code tags.
My questions to start with:
Is this a school assignment or something else?
Is it required to use a C style "char array" and parallel arrays to store the information?
Have you studies "structs"?
As
AbstractionAnon pointed out a "std::string" would be much better than the "char array".
What would also work better is a struct that contains all the information on 1 player and in "main" an array of structs.
That said compare this code with what you posted and see which one is easier to read and follow. A few well placed blank lines makes a big difference. Be sure to read all the comments.
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
|
#include<iostream>
#include<iomanip>
using namespace std; // <--- Best not to use.
int main()
{
constexpr int MAXSIZE{ 4 };
char player_name[MAXSIZE]{}; // <--- ALWAYS initialize all your variables.
int runs[MAXSIZE]{};
int inn[MAXSIZE]{};
int time_out[MAXSIZE]{};
int average{}; // <--- Defined, but never used.
for (int i{}; i <= MAXSIZE; i++) // <--- Do you know how many times this will loop?
// Do you know what problem it will cause?
{
cout << "Enter the name of the Player: ";
cin >> player_name[i];
cout << "Enter the Runs of the player: ";
cin >> runs[i];
cout << "Enter the innings of the player: ";
cin >> inn[i];
cout << "Enter the time the player was not out:";
cin >> time_out[i];
}
for (int b{ 1 }; b <= MAXSIZE; b++) // <--- Do you know how many times this will loop?
{
cout <<
"Player name" << setw(10) << "RUNS" << setw(10) << "innings" << setw(10) << "Times not out\n\n"
"--------" << setw(10) << "--------" << setw(10) << "--------" << setw(10) << "--------\n"
<< player_name[b] << setw(10) << runs[b] << setw(10) << inn[b] << setw(10) << time_out[b] << '\n';
}
return 0; // <--- Not required, but makes a good break point for testing.
}
|
Now looking at line 10 it should be:
char player_name[MAXSIZE][MAXNAME}{};
with "MAXNAME" defined as
constexpr int MAXNAME{ 21 };
. Which means that the 2nd dimension has a size of 20 characters for the name and the 1 extra is for the (\0) to end the string.
Then line 20 would be:
std::cin.getline(player_name[i], 21);
.
Have a look at:
https://en.cppreference.com/w/cpp/io/basic_istream/getline
Andy