Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I assign the integers in an input like 1.2.3 to variables in C++?

Here is an example of what I mean.

Input: 10.20.50

a = 10
b = 20
c = 50

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you will need to store the input in a string or char[] and then iterate over the string or char[] and write some code that will identify the separate parts of the input and convert them to ints using stoi().

this would work but just an example (and i think it will not print the final number unless the input is ended with "." but this should give you an idea of what you could do.

std::string i = "";
std::cin >> i;

std::string buffer = "";
for (auto c : i)
{
    if (c != '.')
    {
        buffer += c;
    }
    else
    {
        int num = std::stoi(buffer);
        buffer = "";
        std::cout << num << ", ";
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading