Read double from keyboard, save it to a vector and find the max value : vector subscript indexer « Vector « C++






Read double from keyboard, save it to a vector and find the max value

  
#include <iostream>
#include <vector>

using namespace std;

int main()
{  
   vector<double> salaries;
   bool more = true;
   while (more)
   {  
      double s;
      cout << "Please enter a salary, 0 to quit: ";
      cin >> s;
      if (s == 0)
         more = false;
      else
         salaries.push_back(s);
   }

   double highest = salaries[0];
   int i;
   for (i = 1; i < salaries.size(); i++)
      if (salaries[i] > highest)
         highest = salaries[i];

   for (i = 0; i < salaries.size(); i++)
   {  
      if (salaries[i] == highest) 
         cout << "highest value => ";
      cout << salaries[i] << "\n";
   }

   return 0;
}
  
    
  








Related examples in the same category

1.Use indexer to add elements to a vector
2.Use indexer to reference elements in a vector
3.Loop thourgh all elements in a vector using <
4.Loop through all elements in a vector using operator [] instead of operator *
5.Create another vector that contains a subrange of vector.
6.Raise all values in a vector by a given percentage