这可能是一个非常快速的解决方法,但我无法弄清楚为什么会出现错误。'operator'不匹配
代码:
#include <iostream>
#include <queue>
#include <vector>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(int argc, char *argv[]){
srand (time(NULL));
double randomNumber = (double)(rand() % 100)/100;
string numCars;
cout << "\nPlease enter the number of cars going through the intersection:" << endl;
cout << "->";
getline (cin, numCars);
for(double i=0; i<numCars; i++){
cout << randomNumber << endl;
}
}
的错误是:
traffic.cpp:80: error: no match for ‘operator<’ in ‘i < numCars’
你不能比较字符串和整数。你必须转换一个。我假设你正在对输入做一些检查,这就是为什么它是一个字符串,但如果你不是,只需从一个int开始。 – chris