Advertisement
Nattack

Untitled

Feb 18th, 2019
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. int main () {
  5.  
  6.     //get number of values
  7.     int number_of_values;
  8.     std::cout << "enter number of values: ";
  9.     std::cin >> number_of_values; // at this point, std::cin >> accepts a SPACE delimited string
  10.     std::cin.ignore(); //use this to make cin ignore spaces later
  11.  
  12.     //get values
  13.     std::string values;
  14.     std::cout << "enter " << number_of_values << " values: ";
  15.     std::getline(std::cin, values);     //get a line from cin, we need to use cin because the '>>'
  16.                                         //operator makes cin only read to the first space,
  17.                                         //whereas getline reads until newline
  18.  
  19.     //read the values and output the max
  20.     std::stringstream getvals(values); //initialize a stringstream with the string "values"
  21.     int maxvalue = 0, value, count = 0;
  22.     while ( count < number_of_values ) {
  23.         getvals >> value;   // using a stringstream is exactly like using cin/cout, << reads input from a variable and stores it
  24.                             // >> takes the first space delimited thing and stores it in the variable
  25.        
  26.         if (value > maxvalue) { // this is the simplest way to get the maximum value
  27.             maxvalue = value;
  28.         }
  29.         count++;
  30.     }
  31.     std::cout << "The max value is " << maxvalue << "\n";
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement