-4

Top Scoring Batsman Name - C++

The runs scored by N batsmen of a cricket team is passed as the input to the program. The program must print the name of the batsman who scored the highest runs. (You can assume that no two batsmen will be the top scorers)

#include <bits/stdc++.h>

using namespace std;

int main(int argc, char** argv)
{
//Defines no of batsmen
int n;
cin>>n;
//Define names of batsmen and their runs
string s[100];
int r[100];
//Getting inputs
for(int i=0;i<n;i++){
scanf("%s,%d",&s[i],&r[i]);
}
// //Assign a value to int max variable
int max=r[0];
int location=0;
//find max value
for(int i=1;i<n;i++){
if(r[i] > max){
    max = r[i];
    location = i;
}
}
cout<<s[location];
}

Input Format: The first line denotes the value of N. Next N lines will contain the name of the batsman and the runs score (both separated by a comma) .

Output Format: The first line contains the name of the batsman with the top score.

Example Input/Output 1:

Input: 
5 
BatsmanA,45 
BatsmanB,52 
BatsmanC,12 
BatsmanD,9 
BatsmanE,78  

Output: BatsmanE
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
ashwin2125
  • 11
  • 2
  • 5
  • 5
    Hello, SO won't complete an assignment, but we can answer specific questions you might have about programming. Please edit this so that you're asking about a concept. – joshwilsonvu Apr 21 '20 at 15:27
  • 3
    **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Asteroids With Wings Apr 21 '20 at 15:32
  • May I also recommend a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? So you can learn how to write proper C++ instead of this strange mix with C-functions and style. – Lukas-T Apr 21 '20 at 16:27

1 Answers1

1

You cannot read a std::string using scanf since scanf is a C function and knows nothing about std::string which is a C++ class.

string s[100];

scanf("%s,%d",&s[i],&r[i]);

Use C++ I/O instead

char comma;
cin >> s[i] >> comma >> r[i];

instead.

john
  • 85,011
  • 4
  • 57
  • 81