开发者

Minimum value of vector displays as 0

I'm trying to display both the maximum and minimum value of a vector (along with other things). The max value displays fine, but the minimum value constantly comes up as 0. The only time it's not 0, it ends up displayin开发者_JS百科g the final value of the list of data.

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    fstream file;
    file.open("dataWin.txt");

    vector<float> dist;
    while(file.eof() == false)
    {
        string miles;
        getline(file, miles);
        float milesNum = atof(miles.c_str());
        dist.push_back(milesNum);
    }

    float max = 0.0;
    float min = 1.0;
    float sum = 0.0;
    float secHour = 3600;
    float lastRecord = 238857;

    for(int i = 0; i < dist.size(); i++)
    {
        if(dist[i] < min)
            min = dist[i];
        if(dist[i] > max)
            max = dist[i];
        sum += dist[i];
    }

    float avg = (float)sum / dist.size();
    float lastSeconds = (float)lastRecord / (float)avg;
    float deadline = (float)lastSeconds / (float)secHour;   

    cout << "National Aeronautics and Space Administration - NASA" << endl;
    cout << "EYES ONLY" << endl << endl;
    cout << "Statistics on solar object 326 Alba" << endl;
    cout << "Danger level: HIGH" << endl << endl;
    cout << "Maximum travel distance: " << max << " miles per second." << endl;
    cout << "Minimum travel distance: " << min << " miles per second." << endl;
    cout << "Average travel distance: " << avg << " miles per second." << endl << endl;
    cout << "WARNING! OBJECT 326 Alba WILL IMPACT IN " << deadline << " HOURS!" << endl;

    file.close();

    return 0;
}

How can I get the actual min value of the list to display?


The problem is that when input ends the getline call will fail and it will not update the string, you are then converting an empty string to a float which is failing in the atof call (if atof fails, it will return 0.0) and pushing that value into the vector. The minimum value in the vector is now 0 and the algorithm is correctly yielding it as the minimum.

If you are going to code that in C++, and assuming that the only contents of the input are floats and spaces, you can make the code simpler by reading directly into a float:

float miles;
while ( std::cin >> miles ) {
   dist.push_back( miles );
}

Even simpler, you can avoid the loop altogether by using iterators and algorithms from the STL:

std::vector<float> dist;
std::copy( std::istream_iterator<float>( std::cin ), std::istream_iterator<float>(),
           std::back_inserter( dist ) );
std::cout << "Max: " << *std::max_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Min: " << *std::min_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Sum: " << std::accumulate( dist.begin(), dist.end() ) << std::endl;

This is just to show what can be done with existing algorithms. In your particular case I would use the std::copy and std::istream_iterators for reading the input, but for processing the already read numbers I would unroll a manual loop (so that all min, max and sum can be calculated in one pass, the STL examples above do it in three passes).


This:

while(file.eof() == false)
{
    string miles;
    getline(file, miles);

should be:

string miles;
while( getline(file, miles) )
{

Where are you picking up the idea you need to call eof() from? You don't, and doing so in the way you are doing it is wrong!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜