Advertisement
krstoilo

Invalid input

Oct 6th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6.  
  7. int sumNumbers (std::vector <int> vec){
  8.  
  9.     int sum = 0;
  10.  
  11.     for(int number : vec){
  12.  
  13.         sum += number;
  14.     }
  15.  
  16.     return sum;
  17. }
  18.  
  19.  
  20. std::vector <int> extractNums (std::string lineStr){
  21.  
  22.  
  23.     std::istringstream istrLine(lineStr);
  24.     std::vector <int> numbers;
  25.     int foundNumb;
  26.     std::string foundWords;
  27.  
  28.     while(!istrLine.eof()){
  29.  
  30.         istrLine >> foundWords;
  31.         if(std::stringstream(foundWords) >> foundNumb){
  32.  
  33.             numbers.push_back(foundNumb) ;
  34.             foundWords = "";
  35.         }
  36.     }
  37.  
  38.     return numbers;
  39.  
  40. }
  41.  
  42.  
  43. int main(){
  44.  
  45.     std::string line;
  46.     getline(std::cin,line);
  47.  
  48.     std::vector <int> numbers = extractNums(line);
  49.  
  50.     std::cout << sumNumbers(numbers) << std::endl;
  51.  
  52.     std::istringstream lineStrm(line);
  53.     std::string words;
  54.     std::string printWord;
  55.  
  56.     while(lineStrm >> words){
  57.  
  58.         if(isalpha(words[0])){
  59.  
  60.             printWord += words + " ";
  61.         }
  62.     }
  63.  
  64.     std::cout << printWord;
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement