mercredi 11 décembre 2019

How does the input operator (>>) know where to stop and take the next input if the inputs are seperated by single spaces?

#include<iostream>
int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl;
    return 0;
}

If the input given is 5 6 It'll print "The sum of 5 and 6 is 11". But how does the istream know when to stop? Does it keeps on taking input if its an integer and stops if it gets another datatype? How does it assign the correct values of v1 and v2?

Aucun commentaire:

Enregistrer un commentaire