Answer to Question #242315 in C++ for Astor

Question #242315

Owner of a Hall

 



Write a C++ program to find the owner name of the hall by hall number using the map.

 

In the main method, obtain input as CSV from the user in the console and find the Hall number to get the Owner of that hall using the map. 

 

Problem constraints:

Create a Map as  map <string, string >

Use find()  to find the hall owner’s name.


Input and Output Format:

The First line of input consists of an Integer 'n' which corresponds to the number of Halls.

The next 'n' lines of Inputs are in CSV format.

For output refer to sample Input and Output for formatting specifications.

 

Sample Input and Output 1:

Enter the number of Halls

4

Enter details of Hall 1:

Hall-230, Binny

Enter details of Hall 2:

Hall-233, Ram

Enter details of Hall 3:

Hall-255, Annie

Enter details of Hall 4:

Hall-240, Vikram

Enter the Hall number to find its owner:

Hall-233

The owner of Hall-233 is Ram

 


1
Expert's answer
2021-09-25T17:46:15-0400
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main() {
    map<string, string> hallToOwner;

    int numHalls = 0;
    cout << "Enter the number of Halls" << endl;
    cin >> numHalls;

    string owner;
    string hall;
    for (int i = 1; i <= numHalls; i++) {
        cout << "Enter details of Hall " << i << ":" << endl;
        cin >> owner >> hall;
        owner = owner.substr(0, owner.size() - 1);
        hallToOwner[owner] = hall;
    }

    cout << "Enter the Hall number to find its owner:" << endl;
    cin >> hall;
    cout << "The owner of " << hall << " is " << hallToOwner[hall] << endl;

    return 0;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS