I am writing a program to record score of a cricket match. One array stores information of batting teams and the other array stores information of bowling team.
The program has to read the information of the teams and depending upon the user’s choice, it must displays either the batting team or the bowling team’s information.
My program does not display the information correctly.
What is the problem?

#include<iostream.h>
struct batting{
char name[20];
int score;
int out;
};

struct bowling{
char name[20];
int maiden;
int runs;
int wickets;
};

int main()
{

 int i,j;
 cout<<"How many number of batting&bowling team:";
 cin>>i;

 batting *bat=new batting[i];
 batting *bat1;
 bowling *bow=new bowling[i];
 bowling *bow1;


 for(j=0;j<i;j++)
 {
 bat1=&bat[j];
 cout<<"\nEnter name:";
 cin.getline((*bat1).name,20);
 cin.ignore(1000,'\n');
 //cin.ignore();
 cout<<"\nEnter score:";
 cin>>(*bat1).score;
 cout<<"\nIndication out(out=1&in=0)";
 cin>>(*bat).out;
 }
  for(j=0;j<i;j++)
 {
 bow1=&bow[j];
 cout<<"\nEnter name:";
 cin.getline((*bow1).name,20);
 cin.ignore(1000,'\n');
 cout<<"\nEnter maiden:";
 cin>>(*bow1).maiden;
 cout<<"\nEnter no: of runs";
 cin>>(*bow1).runs;
 cout<<"\nEnter wickets no:";
 cin>>(*bow1).wickets;
 }


 int k=0;

 cout<<"\n*******Which Information You would like to know?*******";
 cout<<"\nEnter 1 for Batting team && 2 for bowling team";
 cin>>j;
 if(j==1)
 for(k=0;k<i;k++)
 {
  bat1=&bat[k];
  cout<<"\nName:"<<(*bat1).name;
  cout<<"\nScore:"<<(*bat1).score;
  cout<<"\nOut:"<<(*bat1).out;
 }
   return 0;
 }

Recommended Answers

All 3 Replies

I don't see any obvious problem. Would you like to tell us what output you are getting and why you don't like it?

I think you're having trouble with this code because you're using cin.ignore() somewhere in your code, I can't figure out anything else which is wrong ...

This code should work:

#include<iostream.h>
struct batting{
char name[20];
int score;
int out;
};

struct bowling{
char name[20];
int maiden;
int runs;
int wickets;
};

int main()
{

 int i,j;
 cout<<"How many number of batting&bowling team:";
 cin>>i;

 batting *bat=new batting[i];
 batting *bat1;
 bowling *bow=new bowling[i];
 bowling *bow1;

 cin.ignore(1000, '\n');
 for(j=0;j<i;j++)
 {
 bat1=&bat[j];
 cout<<"\nEnter name:";
 cin.getline((*bat1).name,20);
 cout<<"\nEnter score:";
 cin>>(*bat1).score;
 cout<<"\nIndication out(out=1&in=0)";
 cin>>(*bat).out;
 }
 cin.ignore(1000,'\n');
 for(j=0;j<i;j++)
 {
 bow1=&bow[j];
 cout<<"\nEnter name:";
 cin.getline((*bow1).name,20);
 cout<<"\nEnter maiden:";
 cin>>(*bow1).maiden;
 cout<<"\nEnter no: of runs";
 cin>>(*bow1).runs;
 cout<<"\nEnter wickets no:";
 cin>>(*bow1).wickets;
 }


 int k=0;

 cout<<"\n*******Which Information You would like to know?*******";
 cout<<"\nEnter 1 for Batting team && 2 for bowling team";
 cin>>j;
 if(j==1)
 for(k=0;k<i;k++)
 {
  bat1=&bat[k];
  cout<<"\nName:"<<(*bat1).name;
  cout<<"\nScore:"<<(*bat1).score;
  cout<<"\nOut:"<<(*bat1).out;
 }
   return 0;
 }

What did I change?
> First I removed all cin.ignore() statements out of your code ...
> Secondly I added cin.ignore(1000, '\n'); on line 27 and on line 38 ...

Hope this helps !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.