How to count only integers in a string

banksters

I have this small piece of code that I'm trying to work on that takes in a string, and adds one to a counter for every number in the string separated by a space. However, if it runs into something that is not an integer, it immediately stops the counter which isn't what I want it to do.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int counter;

int main()
{           
cout << "Please input string: ";
string input;
getline(cin, input);
istringstream ss(input);

    int num;
    while(ss >> num)
    {
        counter++;
    }

    cout << "Amount of integers in string: " << counter << endl;

}

For example, if I input the string 3 4 5, it will correctly return 3 integers in the string, but if I input something like 3 4 x 6 t 8, it will say I only had two integers in the string.

How do I fix this?

molbdnilo

ss >> num will put the stream in an error state as soon as it encounters a non-integer, which will make while (ss >> num) terminate.

Like many other problems, this can be solved by introducing a level of indirection of some sort.
In this case, you can use indirection through a second stream:

istringstream ss(input);
string word;
while (ss >> word)
{
    istringstream maybenumber(word);
    int number = 0;
    if (maybenumber >> number)
    {
        counter++;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to ensure a textbox only accepts integers (and not string)

From Dev

How to count only letters in a string?

From Dev

Count how many individual integers there are in a string, Python 3

From Dev

Java: Count the number of integers in a string

From Dev

C++ how to get ONLY integers from complex string

From Dev

C++ how to get ONLY integers from complex string

From Dev

How to split a String into integers?

From Dev

JavaScript: Count instances of specific integers in a string

From Dev

How to only allow Integers into a textbox

From Dev

Java: string split by regexp to get only integers

From Dev

How to validate an input string allowing only integers that are 9, 10, 12, or 13 digits long?

From Dev

How to split() a string to an array of integers

From Dev

How to convert string to a list of integers?

From Dev

how to replace characters in a string with integers?

From Dev

How to identify the integers in a string in Python?

From Dev

How to generate list of random integers, but only using specified integers? (Python)

From Dev

How to convert string input into tuple with integers and a string

From Dev

How to let fminsearch only search over integers?

From Dev

How to get only positive integers in array?

From Dev

How do I count the words in a string using only loops and string.strip()?

From Java

How to count string occurrence in string?

From Dev

Check if String has only Integers separated by comma in PHP

From Dev

Convert to Number from a String but only strings that are valid integers

From Java

How to parse sequence of integers from string in C?

From Dev

How to strip string of all integers and spaces

From Dev

How to concatenate array of integers into comma separated string

From Dev

How to concat a list of integers to a string in Erlang?

From Dev

How to convert array of Integers into comma separated string

From Dev

How to extract two integers from a string?

Related Related

  1. 1

    How to ensure a textbox only accepts integers (and not string)

  2. 2

    How to count only letters in a string?

  3. 3

    Count how many individual integers there are in a string, Python 3

  4. 4

    Java: Count the number of integers in a string

  5. 5

    C++ how to get ONLY integers from complex string

  6. 6

    C++ how to get ONLY integers from complex string

  7. 7

    How to split a String into integers?

  8. 8

    JavaScript: Count instances of specific integers in a string

  9. 9

    How to only allow Integers into a textbox

  10. 10

    Java: string split by regexp to get only integers

  11. 11

    How to validate an input string allowing only integers that are 9, 10, 12, or 13 digits long?

  12. 12

    How to split() a string to an array of integers

  13. 13

    How to convert string to a list of integers?

  14. 14

    how to replace characters in a string with integers?

  15. 15

    How to identify the integers in a string in Python?

  16. 16

    How to generate list of random integers, but only using specified integers? (Python)

  17. 17

    How to convert string input into tuple with integers and a string

  18. 18

    How to let fminsearch only search over integers?

  19. 19

    How to get only positive integers in array?

  20. 20

    How do I count the words in a string using only loops and string.strip()?

  21. 21

    How to count string occurrence in string?

  22. 22

    Check if String has only Integers separated by comma in PHP

  23. 23

    Convert to Number from a String but only strings that are valid integers

  24. 24

    How to parse sequence of integers from string in C?

  25. 25

    How to strip string of all integers and spaces

  26. 26

    How to concatenate array of integers into comma separated string

  27. 27

    How to concat a list of integers to a string in Erlang?

  28. 28

    How to convert array of Integers into comma separated string

  29. 29

    How to extract two integers from a string?

HotTag

Archive