'How can I assign the integers in an input like 1.2.3 to variables in C++? [closed]
Here is an example of what I mean.
Input: 10.20.50
a = 10
b = 20
c = 50
Solution 1:[1]
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 << ", ";
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GrimStarGaming |
