From b8a9aa26e4998a52956590faea6542d843c555a7 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Sat, 13 May 2017 19:42:59 -0700 Subject: [PATCH] cpp:io: Added unlimited input example --- cpp/io/unknown_number_of_input.cpp | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 cpp/io/unknown_number_of_input.cpp diff --git a/cpp/io/unknown_number_of_input.cpp b/cpp/io/unknown_number_of_input.cpp new file mode 100644 index 0000000..0f0be1b --- /dev/null +++ b/cpp/io/unknown_number_of_input.cpp @@ -0,0 +1,34 @@ +#include + +//use cout and cin to print some stuff and get input + +int main(){ + /* + * All of these "<<" are going into std::cout + * the "endl" forces a flush + * note that is "l" as in "line" not "1" as in "one" + */ + std::cout << "Enter numbers, press to finish: "<< std::endl; + + int input; + int sum; + input = 0; + sum = 0; + + /* + * For some reason this is deliminated by spaces + * if only one number is given, it will wait for another input + */ + while (std::cin >> input){ + sum += input; + } + + + /* + * formated for legability + * we are putting all those things into std::cout in the order they appear + */ + std::cout << "The sum is " + << sum + << std::endl; +}