0
#include <iostream>
#include <string>
using namespace std;

class Team {
  public:
  string teamName ;
  string playerOne ;
  string PlayerTwo ;
  string playerThree ;
  double totalRunsScored;
};
void welcomeMessage();
void dispTeam(Team t1,Team t2);
void roleSelection(Team t1,Team t2);
int currentBatsmen;
int currentBowler;
int main()
{
    welcomeMessage();
    Team t1,t2 ;
    dispTeam(t1,t2);
    cout<<"\n lets start first innings \n";
    roleSelection(t1,t2);
    return 0;
}

void welcomeMessage(){
cout << "Welcome to gully Cricket \n";
cout<< "\n";
cout<< "\n";
cout<< "\n";
cout << "Enter the name of your team players for Team one and Two\n ";
}
void dispTeam(Team t1,Team t2) {

   getline(cin,t1.playerOne);
   getline(cin,t1.PlayerTwo);
   getline(cin,t1.playerThree);
   cout << "\n Team one \t" <<"\t" <<t1.playerOne << "\t" << t1.PlayerTwo << "\t" << t1.playerThree <<"\t" << endl ;

   getline(cin,t2.playerOne);
   getline(cin,t2.PlayerTwo);
   getline(cin,t2.playerThree);
   cout << "\n Team Second \t" << "\t" <<t2.playerOne <<"\t"<< t2.PlayerTwo <<"\t"<< t2.playerThree <<"\t" << endl ;
}
void roleSelection(Team a,Team b){
    cout<<"Choose Your batsmen from Team one press 1,2,3 : \n";
    cin >> currentBatsmen;
    if (currentBatsmen==1){
     cout<<a.playerOne;
    }else if (currentBatsmen==2){
    cout<<"you have chosen \t" << a.PlayerTwo <<" \t as your batsmen";
    }else if(currentBatsmen==3){
    cout<<"you have chosen \t "<< a.playerThree <<" \t as your batsmen";
    }
}

in this function roleselection() my cout is showing blank and i am not able to understand why? above you can see my code as suggested i have included string header file too and also iostream. It works well in dispTeam() function.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • 1
    Define "showing blank". Is there no output at all? Is there output but the player strings are blank? Is the prompt output but the choice reports not, or vice-versa, or some combination? What inputs have you tested, and what are the results in each case? – underscore_d Jun 17 '20 at 11:20
  • `std::endl` sends a newline and a `std::flush` (which is what makes your text show up) . Either change to `endl` or add a `<< flush` – Botje Jun 17 '20 at 11:22

1 Answers1

2

The problem is in how you pass the arguments to your functions:

void dispTeam(Team t1,Team t2);

This means that the Team object are being passed by value, which means the local argument variables inside the function will be copies of the original object. The function will then continue to modify these copies, which are independent and distinct from the original objects.

You need to pass by reference to pass references to the original objects:

// Note ampersands here and here, meaning pass by reference
//                v        v
void dispTeam(Team& t1,Team& t2);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621