Проблем със задача 04.Noise от C++ Fundamentals
В задачата не знам как да отделя int променливите от string низа.Пробвах с typeid ,,но не се получава.
Помощ?
Условие: Write a program that reads a positive integer number and returns its square root (print the result as cout prints
double numbers)
The number will be entered with “noise” in it, i.e. there will be symbols that are not digits. These symbols should be
ignored. The last symbol of the input of the number will always be . (dot) and there will be no other . (dot) in the
number.
Мойто решение:
#include<iostream>
#include<iterator>
#include<algorithm>
#include<iterator>
#include<string>
#include<vector>
using namespace std;
int convertToNumber(vector<int> vec) {
reverse(vec.begin(), vec.end());
int decimal = 1;
int total = 0;
for (auto& it : vec) {
total += it * decimal;
decimal *= 10;
}
return total;
}
int main() {
vector<char> sequence;
char letter=' ';
while (letter!=' . ') {
cin >> letter;
sequence.push_back(letter);
}
vector<int> intsequence;
for (int j = 0; j < sequence.size(); j++) {
if (typeid(sequence[j]).name()=="int") {
intsequence.push_back(sequence[j]);
}
}
int a = convertToNumber(intsequence);
cout << sqrt(a);
return 0;
}