从文本文件中读取数字,并检查它们是否在范围内

时间:2016-11-29 12:39:52

标签: c++ vector

我正在尝试加载一个包含1000个数字或类似内容的文本文件,如果它们在范围内,我想检查每个数字。我尝试使用矢量,但不能让我直接。

所以基本上我想检查第一个数字是否更大或者=到下一个数字,并且多次这样做。

ps:我对任何类型的编程都非常陌生,所以请不要吝啬:))

这是我的代码:

using namespace std;

int main() {
    ifstream myfile;
    myfile.open ("A1.txt");

    vector<int>array;
    int count;
    double tmp;
    int i;

    myfile >> tmp;
    while (int i = 0; i < count; i++){
        myfile >> tmp;
        array.push_back(tmp);
        cout << count;
    }

    myfile.close();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我会建议一个循环,直到你读完每个数字:

...
double previous, tmp;
myfile >> previous;
array.push_back(previous);//this inserts the first element
//but you have to decide if you need it or not, because it is not compared with any value
while (myfile >> tmp){
    array.push_back(tmp);
    if(previous >= tmp){
        //Do something;
    }
    previous = tmp;//the next previous is the current tmp
}
...