Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- int main () {
- //get number of values
- int number_of_values;
- std::cout << "enter number of values: ";
- std::cin >> number_of_values; // at this point, std::cin >> accepts a SPACE delimited string
- std::cin.ignore(); //use this to make cin ignore spaces later
- //get values
- std::string values;
- std::cout << "enter " << number_of_values << " values: ";
- std::getline(std::cin, values); //get a line from cin, we need to use cin because the '>>'
- //operator makes cin only read to the first space,
- //whereas getline reads until newline
- //read the values and output the max
- std::stringstream getvals(values); //initialize a stringstream with the string "values"
- int maxvalue = 0, value, count = 0;
- while ( count < number_of_values ) {
- getvals >> value; // using a stringstream is exactly like using cin/cout, << reads input from a variable and stores it
- // >> takes the first space delimited thing and stores it in the variable
- if (value > maxvalue) { // this is the simplest way to get the maximum value
- maxvalue = value;
- }
- count++;
- }
- std::cout << "The max value is " << maxvalue << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement