Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a single variable store multiple values?

Tags:

c++

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.

like image 544
elementalneil Avatar asked Mar 13 '26 05:03

elementalneil


1 Answers

int num; stores only one number at a time.

cin >> number replaces the stored number with the next number from input.

like image 146
HolyBlackCat Avatar answered Mar 15 '26 20:03

HolyBlackCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!