cpp:io: Added unlimited input example
This commit is contained in:
parent
dc6fbd67ed
commit
b8a9aa26e4
34
cpp/io/unknown_number_of_input.cpp
Normal file
34
cpp/io/unknown_number_of_input.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include<iostream>
|
||||||
|
|
||||||
|
//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 <ctrl-d> 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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user