我有一个array
,其中包含不同时间的教室数据和不同的氧气读数。因此,用户必须输入特定的教室和时间,我的程序将打印出当时教室的氧气读数。但是,我有error
,我无法将string array
与string
进行比较。有人能帮帮我吗?
cout << "Please enter the classroom: ";
string classroom;
cin >> classroom;
cout << "Please enter the time: ";
string time;
cin >> time;
cout << endl;
string myData[6][13] = {
{ "EN10-5-56" , "500", "700", "2000", "1850", "2000", "800", "750", "2300", "1900", "1600", "750", "700" },
{ "EN10-5-65" , "550", "1600", "1700", "780", "1980", "700", "750", "2300", "780", "800", "650", "550" },
{ "EN14-5-83" , "600", "700", "2000", "1850", "2000", " 600", "650", "700", "200", "800", "750", "700" },
{ "EN14-5-69" , "900", "600", "1950", "780", "800", "590", "620", "760", "1900", "1890", "2000", "700" },
{ "EN18-5-112", "550", "1670", "2000" , "680", "900", "1500", "1800", "2500", "779", "800", "650", "650" },
{ "EN18-5-118", "800", "1890", "2300", "1850", "2000", "800", "750", "1700", "560", "1300", "1500", "850" },
}; // row x column (class x time)
// row x column
for (int i = 0; i < 6; i++) // classrooms (row)
{
for (int j = 0; j < 13; j++)
{
if (myData[i] == classroom) // ERROR HERE
{
cout << myData[i] << endl;
}
}
}
错误是:
错误C2679二进制&#39; ==&#39;:找不到带有&#39; std :: string&#39;类型的右手操作数的运算符(或者没有可接受的转换),(myData [i] ==教室)。
我如何找到使用输入时间来获取读数?有什么想法??
答案 0 :(得分:0)
myData
是一个二维数组,但您只提供一个维度说明符。
myData[i]
将为您提供array-of-string
,而不是单个string
实例。您使用i
作为列,因此您还必须为该行提供j
。
if( myData[i][j] == classroom ) {
cout << myData[i][j] << endl;
}
答案 1 :(得分:0)
它是一个2D矩阵。您无法将其元素作为myData[i]
访问。你必须给两个索引。
将myData[i]
替换为myData[i][0]
,并将其与classroom
进行比较。
如果条件允许,请将myData[i]
替换为myData[i][j]
。
现在应该可以了。
答案 2 :(得分:0)
myData
不是1-dimensional array
,而是2-dimensional array
。
所以,你必须这样做:
cout << "Please enter the classroom: ";
string classroom;
cin >> classroom;
cout << "Please enter the time: ";
string time;
cin >> time;
cout << endl;
string myData[6][13] = {
{ "EN10-5-56" , "500", "700", "2000", "1850", "2000", "800", "750", "2300", "1900", "1600", "750", "700" },
{ "EN10-5-65" , "550", "1600", "1700", "780", "1980", "700", "750", "2300", "780", "800", "650", "550" },
{ "EN14-5-83" , "600", "700", "2000", "1850", "2000", " 600", "650", "700", "200", "800", "750", "700" },
{ "EN14-5-69" , "900", "600", "1950", "780", "800", "590", "620", "760", "1900", "1890", "2000", "700" },
{ "EN18-5-112", "550", "1670", "2000" , "680", "900", "1500", "1800", "2500", "779", "800", "650", "650" },
{ "EN18-5-118", "800", "1890", "2300", "1850", "2000", "800", "750", "1700", "560", "1300", "1500", "850" },
}; // row x column (class x time)
// row x column
for (int i = 0; i < 6; i++) // classrooms (row)
{
for (int j = 0; j < 13; j++)
{
if (myData[i][j] == classroom) // NO ERROR HERE
{
cout << myData[i] << endl;
}
}
}