Sir_Martins
Sir_Martins

Reputation: 27

C++ Multidimensional string arrays

Can someone please correct this code for me, so it can produce the correct output. The code is to display the name of the patient, the doctor that treated him/her, the room where he/she was treated.

#include <iostream> 
using namespace std;

int main()
{
    string bisi[3][4] = {{"      ", "DOCTOR 1", "DOCTOR 2", "DOCTOR 3"},
{"ROOM 1", "AFUAH", "ARABA", "JOHNSON"},
{"ROOM 2", "BENJAMIN", "KOROMA", "CHELSEA"}};

    for (int row=0; row<3; row++){
        for (int col=0; col<4; col++){
            cout<<bisi [row][col]<<" "; /*I get error on this line.The angle bracket "<<" Error Message: No operator matches this operand.*/
        }
        cout<<endl;
    }

    return 0;
}

Upvotes: 0

Views: 4815

Answers (3)

Sir_Martins
Sir_Martins

Reputation: 27

I just added the preprocessor directive #include <"string"> without the quotes and it worked fine. Thank You guys for helping out.

Upvotes: 0

DimChtz
DimChtz

Reputation: 4343

You need to change:

cout << bisi[row] << bisi[col] << " ";

to:

 cout << bisi[row][col] << " ";

bisi is a 2d array, bisi[row] or bisi[col] will just print you an address

Upvotes: 3

Lanting
Lanting

Reputation: 3068

From an object oriented view point, this is bad style. Wrap the information in a class. e.g.

struct Appointment
{
  std::string patient;
  std::string doctor;
  std::string room;
}

and store that information in some kind of collection:

std::vector<Appointment> appointments;
appointments.emplace_back({"henk", "doctor bob", "room 213"});
appointments.emplace_back({"jan", "doctor bert", "room 200"});

printing could then be done by:

for (const auto &appointment: appointments)
{
  std::cout << appointment.patient
            << appointment.doctor
            << appointment.room
            << std::endl;
}

Upvotes: 1

Related Questions