2016-03-30 63 views
0

有人可以为我纠正这个代码,所以它可以产生正确的输出。 代码是显示患者的姓名, 医生对他/她的治疗, 他/她治疗的房间。C++多维字符串数组

#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; 
} 
+1

您能否确定'bisi'的每个元素应该是什么意思(以及它们的关系应该是什么)。也许向我们展示你想看到的输出。 –

+1

我接受这个作为初学者的测试程序。但请注意,惯用的C++不会像这样在多维(C ...)数组中建模患者/医生/房间关系。如果您使用C++利用其功能和库,您将拥有'class Doctor','class Patient',... – DevSolar

+0

。使用一个向量。如果不是简单地使用普通的C。 – blade

回答

3

你需要改变:

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

到:

bisi是一个二维数组,bisi[row]bisi[col]将只打印您的地址

+0

我用cout << bisi [row] [col] <<“”;但我得到这个“<<”错误@DimChtz –

+0

@Sir_Martins你能用新的代码和你得到的错误更新这个问题吗? – DimChtz

+0

我将我的数据类型从“字符串”更改为“int”,代码正常工作,没有错误。 –

1

从一个对象以人为本的观点,这是不好的风格。将信息包装在一个班级中。例如

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

,并存储在某种收集的信息:

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

印刷然后可以通过做:

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

我刚添加的预处理指令#include < “串” >没有引号,它工作正常。 谢谢你们帮忙。