-3

I have a script here that counts even numbers in an int type var.

#include <iostream>
#include <string>

int main()
{
    std::cout << "Type a string: " << std::endl;

    std::string s;
    std::cin >> s; 

    unsigned int digits = 0, evens = 0;

    for ( std::string::size_type i = 0; i < s.size() && s[i] >= '0' && s[i] <= '9'; i++ )
    {
        ++digits;
        evens += ( s[i] - '0' ) % 2 == 0;
    }        

    std::cout << "The number has " << digits << " digit(s)." << std::endl;
    std::cout << "The number has " << evens << " even digit(s)." << std::endl;

    return 0;
}

Im trying to find a way to turn this into a string instead so I can count on how many even numbers or numbers are there in that string?

Type a string:
29 coaches 28

Even: 1
Found Numbers: 1

In python it should be something like:

s = "75,41,14,8,73,45,-16"
evenNumbers = []

for number in s.split(","):
    int_num = int(number)
    if int_num % 2 == 0 and int_num > 0:
        evenNumbers.append(int_num)

print("Even Numbers : \"{}\"".format(evenNumbers))

But I dont know how to do it in C++

  • `std::cin >> s;` is the problem. This only reads a word (i.e. `29`). You want `std::getline( std::cin, s );` – ChrisMM Dec 19 '20 at 02:31
  • 1
    And you can't write `&& s[i] >= '0' && s[i] <= '9'` in for statement which will stop the loop if there's non-number letter come. – ElapsedSoul Dec 19 '20 at 02:34
  • What _is_ your problem in C++ then? To me it looks like you've firgured it out. – Ted Lyngmo Dec 19 '20 at 02:35
  • 1
    Your title and question state "even numbers", but your code output claims "even digits". They're not synonymous unless you're dealing with single-digit numbers. So... which is it supposed to be? The python code is numbers, not digits. Is that the goal? – WhozCraig Dec 19 '20 at 02:37
  • My problem is that for example, 24 is two even digits. 1 Big Brown 24 Fox. There should only be 2 even integers – Shu Herm Dec 19 '20 at 02:42
  • Draw it on a paper. Still a problem? – Ted Lyngmo Dec 19 '20 at 02:50
  • if you are counting 24 as two distinct digits you need to ask it as counting even digits in a string because when you ask counting even numbers in a string it will mean 24 as one even number, – Abdurrahim Jan 11 '21 at 21:38

2 Answers2

0

Your loop will stop on first non-numeric character: i < s.size() && s[i] >= '0' && s[i] <= '9'

Also, you are counting also alphanumerics too. So your loop needs fixing first. I recommend using regex for such problems it will be much generic:

const std::regex numbers("\\-?\\d+");

for (auto it = std::sregex_iterator(s.begin(), s.end(), numbers); it != std::sregex_iterator(); it++) { 
    ++digits;
    char lastCharacterOfMatchingString = *it->str(0).rbegin();
    evens += (lastCharacterOfMatchingString - '0') % 2 == 0;
}
Abdurrahim
  • 2,078
  • 1
  • 17
  • 23
0

This sounds like 3 requirements:

  1. split a string based on a space: see How do I iterate over the words of a string?
  2. try convert the words to numbers - can use std::stoi() for this
  3. count the even numbers - can use modulus for this

So something like:

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>

int main()
{
    //0 - Get input
    std::cout << "Type a string: " << std::endl;
    std::string s;
    std::getline(std::cin, s) ;
    
    //1 - split
    std::istringstream iss(s);
    std::vector<std::string> words{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
    
    std::vector<int> res;   //store the results as per the python example
    for(auto it: words){
        int val = 0;
        try{
            val = std::stoi(it, nullptr);   //2 - convert
        }
        catch (const std::invalid_argument& ia) { /* what to do with words */ }
        if ((val != 0) && (val % 2 == 0))
            res.push_back(val);     //3 - count
    }
    
    std::cout << "even numbers: " << std::to_string(res.size()) << std::endl;
    return 0;
}
myk
  • 708
  • 2
  • 8
  • 20