take different inputes within a array from user

SO i was writing a program to take input from user of the batting figures of the cricket team and display it latter using array .But after the first input i.e Player_name .the whole codes gets executed and doesnt take any other input from the user.

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
  #include<iostream>
#include<iomanip>
using namespace std;
int main(){
    char player_name[4];
    int runs[4];
    int inn[4];
    int time_out[4];
    int average;
    
    for(int i=0;i<=4;i++){
        cout<<"Enter the name of the Player\n";
        cin>>player_name[i];
        cout<<"Enter the Runs of the player\n";
        cin>>runs[i];
        cout<<"Enter the innings of the player\n";
        cin>>inn[i];
        cout<<"Enter the time the player was not out\n";
        cin>>time_out[i];
        
    }
 
 for(int b=1;b<=4;b++){
     cout<<"Player name"<<setw(10)<<"RUNS"<<setw(10)<<"innings"<<setw(10)<<"Times not out"<<endl;
     cout<<endl;
     cout<<"--------"<<setw(10)<<"--------"<<setw(10)<<"--------"<<setw(10)<<"--------"<<endl;
     cout<<player_name[b]<<setw(10)<<runs[b]<<setw(10)<<inn[b]<<setw(10)<<time_out[b]<<endl;
     
 }
    
   return 0; 
    
}
L4: That doesn't reserve 4 player names. That reserves a 4 character array.
You need the following:
1
2
3
#include <string>
...
  string[4];


L13: What are you inputting for player_name? Are you inputting a 2 part name? i.e. John Doe
cin >> stops at the first whitespace (between John and Doe). The last name is left in the input buffer.
L15,17,19: These statements all fail. In each case, you're trying to read an int, but the input buffer contains a last name.
Use getline.
 
  getline (cin, player_name[i]);


L23: Your loop index is wrong. Your array elements are 0-3.
Use:
 
  for (int b=0; b<4; b++)
Last edited on
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
Topic archived. No new replies allowed.