I recently began learning programming using C++, and came across this problem called, 'Life, the Universe, and Everything'. I found a solution from Google, and modified it a bit to understand it better.
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
int number;
while(1){
cin >> number;
if(number==42)
break;
cout << number << " ";
}
getch();
return 0;
}
An output from the above program was:
1 2 88 42 99
1 2 88
Here I do not understand how the variable number stores multiple values from the input stream, and keeps storing values until I press Return. As far as I knew, a variable can only store a single value of it's corresponding data-type.
int num; stores only one number at a time.
cin >> number replaces the stored number with the next number from input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With