Write the below code in C++. Define and initialize a struct to hold each match (
ID: 3765914 • Letter: W
Question
Write the below code in C++.
Define and initialize a struct to hold each match (given in one line).
Define array of struct to keep all matches in the arrays.
Read data from files into your array of structs. To do this, write a function that takes four strings and returns a struct filled with the data given in parameters.
Design a menu for the user who can search matches over four criteria: Date, Match, Venue, and Time.
After each search, ask user if he/she wants to make another search (Yes/No). Then exit in case of No.
A couple example runs are given below. In search operation, * means "no condition". Other than * operator means user is looking for that string in the match data.
Data is loaded. You can make a search now.
Please select your criteria below
Enter criterion for Date:Feb
Enter criterion for Match:*
Enter criterion for Venue:*
Enter criterion for Time:*
13 match(es) found
Feb 19, 2011 Bangladesh vs India Dhaka 1400
Feb 20, 2011 New Zealand vs Kenya Chennai 0930
Feb 20, 2011 Sri Lanka vs Canada Hambantota 1430
Feb 21, 2011 Australia vs Zimbabwe Ahmedabad 1430
Feb 22, 2011 England vs Netherlands Nagpur 1430
Feb 23, 2011 Pakistan vs Kenya Hambantota 1430
Feb 24, 2011 South Africa vs West Indies New Delhi 1430
Feb 25, 2011 Australia vs New Zealand Nagpur 0930
Feb 25, 2011 Bangladesh vs Ireland Dhaka 1400
Feb 26, 2011 Sri Lanka vs Pakistan Colombo 1430
Feb 27, 2011 India vs England Bangalore 1430
Feb 28, 2011 Zimbabwe vs Canada Nagpur 0930
Feb 28, 2011 West Indies vs Netherlands New Delhi 1430
Do you want to make another search(Y/N):Y
Please select your criteria below
Enter criterion for Date:March
Enter criterion for Match:England
Enter criterion for Venue:*
Enter criterion for Time:*
4 match(es) found
March 2, 2011 England vs Ireland Bangalore 1430
March 6, 2011 England vs South Africa Chennai 0930
March 11, 2011 Bangladesh vs England Chittagong 1430
March 17, 2011 England vs West Indies Chennai 1430
Do you want to make another search(Y/N):Y
Please select your criteria below
Enter criterion for Date:Feb
Enter criterion for Match:Canada
Enter criterion for Venue:Nagpur
Enter criterion for Time:*
1 match(es) found
Feb 28, 2011 Zimbabwe vs Canada Nagpur 0930
Do you want to make another search(Y/N):N
This file includes 49 match schedules of World Cup. First two lines are comment lines, and indicated by # at their first character. Rest of them (49 lines) are data lines.
Below is the Text File data.
# Cricket World Cup 2011, Game Schedule
# Date;Match;Venue;Time
Feb 19, 2011;Bangladesh vs India;Dhaka;1400
Feb 20, 2011;New Zealand vs Kenya;Chennai;0930
Feb 20, 2011;Sri Lanka vs Canada;Hambantota;1430
Feb 21, 2011;Australia vs Zimbabwe;Ahmedabad;1430
Feb 22, 2011;England vs Netherlands;Nagpur;1430
Feb 23, 2011;Pakistan vs Kenya;Hambantota;1430
Feb 24, 2011;South Africa vs West Indies;New Delhi;1430
Feb 25, 2011;Australia vs New Zealand;Nagpur;0930
Feb 25, 2011;Bangladesh vs Ireland;Dhaka;1400
Feb 26, 2011;Sri Lanka vs Pakistan;Colombo;1430
Feb 27, 2011;India vs England;Bangalore;1430
Feb 28, 2011;Zimbabwe vs Canada;Nagpur;0930
Feb 28, 2011;West Indies vs Netherlands;New Delhi;1430
March 1, 2011;Sri Lanka vs Kenya;Colombo;1430
March 2, 2011;England vs Ireland;Bangalore;1430
March 3, 2011;South Africa vs Netherlands;Mohali;0930
March 3, 2011;Pakistan vs Canada;Colombo;1430
March 4, 2011;New Zealand vs Zimbabwe;Ahmedabad;0930
March 4, 2011;Bangladesh vs West Indies;Dhaka;1430
March 5, 2011;Sri Lanka vs Australia;Colombo;1430
March 6, 2011;England vs South Africa;Chennai;0930
March 6, 2011;India vs Ireland;Bangalore;1430
March 7, 2011;Kenya vs Canada;New Delhi;1430
March 8, 2011;Pakistan vs New Zealand;Pallekele;1430
March 9, 2011;India vs Netherlands;New Delhi;1430
March 10, 2011;Sri Lanka vs Zimbabwe;Pallekele;1430
March 11, 2011;West Indies vs Ireland;Mohali;0930
March 11, 2011;Bangladesh vs England;Chittagong;1430
March 12, 2011;India vs South Africa;Nagpur;1430
March 13, 2011;New Zealand vs Canada;Mumbai;0930
March 13, 2011;Australia vs Kenya;Bangalore;1430
March 14, 2011;Bangladesh vs Netherlands;Chittagong;0900
March 14, 2011;Pakistan vs Zimbabwe;Pallekele;1430
March 15, 2011;South Africa vs Ireland;Kolkata;1430
March 16, 2011;Australia vs Canada;Bangalore;1430
March 17, 2011;England vs West Indies;Chennai;1430
March 18, 2011;Ireland vs Netherlands;Kolkata;0930
March 18, 2011;Sri Lanka vs New Zealand;Mumbai;1430
March 19, 2011;Bangladesh vs South Africa;Dhaka;0900
March 19, 2011;Australia vs Pakistan;Colombo;1430
March 20, 2011;Zimbabwe vs Kenya;Kolkata;0930
March 20, 2011;India vs West Indies;Chennai;1430
March 23, 2011;First Quarter-final;Dhaka;1400
March 24, 2011;Second Quarter-final;Ahmedabad;1430
March 26, 2011;Third Quarter-final;Dhaka;1400
March 25, 2011;Fourth Quarter-final;Colombo;1430
March 29, 2011;First Semi-final;Colombo;1430
March 30, 2011;Second Semi-final;Mohali;1430
April 2, 2011;FINAL;Mumbai;1430
Explanation / Answer
Input file is file.txt
#include <bits/stdc++.h>
using namespace std;
struct Node{
string Date;
string Match;
string Venue;
string Time;
Node(){
Date="";
Match="";
Venue="";
Time="";
}
};
bool is_present(string s,string s1){
if(s1=="*"){
return true;
}
else{
if(s.find(s1)!=string::npos){
return true;
}
return false;
}
}
int main(){
ifstream inf;
inf.open("file.txt");
string s;
getline(inf,s);
getline(inf,s);
Node arr[49];
for(int i=0;i<49;i++){
getline(inf,s);
int a[3];
int x=0;
for(int j=0;j<s.length();j++){
if(s[j]==';'){
a[x]=j;
x++;
}
}
string s1=s.substr(0,a[0]);
string s2=s.substr(a[0]+1,a[1]-a[0]-1);
string s3=s.substr(a[1]+1,a[2]-a[1]-1);
string s4=s.substr(a[2]+1,s.length()-a[2]);
arr[i].Date=s1;
arr[i].Match=s2;
arr[i].Venue=s3;
arr[i].Time=s4;
}
cout<<"Data is loaded. You can make a search now."<<endl;
bool search=true;
while(search){
string s1,s2,s3,s4;
cout<<"Enter criterion for Date:";
cin>>s1;
cout<<"Enter criterion for Match:";
cin>>s2;
cout<<"Enter criterion for Venue:";
cin>>s3;
cout<<"Enter criterion for Time:";
cin>>s4;
int arr1[49];
int count=0;
for(int i=0;i<49;i++){
if(is_present(arr[i].Date,s1) && is_present(arr[i].Match,s2) && is_present(arr[i].Venue,s3) && is_present(arr[i].Time,s4) ){
arr1[i]=1;
count++;
}
else{
arr1[i]=0;
}
}
cout<<count<<" match(es) found"<<endl;
for(int i=0;i<49;i++){
if(arr1[i]==1){
cout<<arr[i].Date<<" "<<arr[i].Match<<" "<<arr[i].Venue<<" "<<arr[i].Time<<endl;
}
}
cout<<"Do you want to make another search(Y/N):";
cin>>s;
if(s=="N"){
search=false;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.