我有以下for loop
:
string temp;
int responseInt[10][10] = { {0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}};
for (int i = 0; i < numberPros; i++)
{
for (int j = 0; j < numberRes; j++)
{
cout << "Is " << process[i] << " holding (h), requesting (r), or doing nothing (n) to " << resources[j] << " ?: ";
cin >> temp;
if (temp == 'n')
responseInt[i][j] = 0;
else if (temp == 'h')
responseInt[i][j] == -1;
else if (temp == 'r')
responseInt[i][j] == 1;
}
}
但是,如果if
语句被忽略,那就好了,因为responseInt
的默认值永远不会改变,即使我输入h
或{{1} }或r
。
我已经尝试过使用字符串,但同样的事情发生了。
任何帮助都将不胜感激。
答案 0 :(得分:0)
这有效:
#include <string>
#include <iostream>
using namespace std;
int main(){
string temp;
int responseInt[10][10] = { {0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}};
int numberPros = 2;
int numberRes = 10;
for (int i = 0; i < numberPros; i++)
{
for (int j = 0; j < numberRes; j++)
{
cout << "Is " << i << " holding (h), requesting (r), or doing nothing (n) to " << j << " ?: ";
cin >> temp;
if (temp == "n")
responseInt[i][j] = 0;
else if (temp == "h")
responseInt[i][j] == -1;
else if (temp == "r")
responseInt[i][j] == 1;
}
}
}
您的cin>>temp
正在读取字符串,因此最好使用双引号(例如"r"
)而不是单引号(例如'r'
)将其与字符串进行比较。
请注意,我必须包含一堆额外的代码才能编译。这应该是您的问题,作为最低工作示例(MWE)。