Taking inputs

Pages: 12
Recently I've been helped on making a program which counts numbers found in a string.
A little problem though..I'm stuck on how to enable user to Input the string he wants.
Please help






#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
stringstream in( "More like cat 7 dog 42 9000" );
string word;
int counter = 0;
while ( in >> word )
{
if ( word.find_first_not_of( "0123456789" ) == string::npos && stoi( word ) < 1000 ) counter++;
}
cout << "There are " << counter << " numbers less than 1000\n";
}
.I'm stuck on how to enable user to Input the string he wants.


How about this?
1
2
string user_input;
cin >> user_input;
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{ cout<<"Enter string";
string user_input;
cin >> user_input;
int counter = 0;
{
if ( user_input.find_first_not_of( "0123456789" ) == string::npos && stoi( user_input ) < 1000 ) counter++;
}
cout << counter;
}



I tried but the outcome keeps coming 0
Maybe?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
    std::string aString;
    std::cout << "Please enter a string: ";
    std::cin >> aString;
    char ch = '~';
    
    int count = 0;
    
    for( int i = 0; i < aString.length(); i++ )
    {
        ch = aString[i];
        std::cout << ch;
        
        if ( ch >= '0' and ch <= '9')
        {
            count++;
            std::cout << " is a number " << count << '\n';
        }
        else
            std::cout << '\n';
    }
    std::cout << "Count: " << count << '\n';
    
    return 0;
}
My aim is not counting number of digits. It's to count occurrence of number less than 1000 in a string
I tried but the outcome keeps coming 0
Please be more clear...
Going off the post above the one I just quoted,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    cout<<"Enter string: ";
    string user_input;
    cin >> user_input;
    int counter = 0;
    {
        if ( user_input.find_first_not_of( "0123456789" ) == string::npos && stoi( user_input ) < 1000 )
            counter++;
    }
    cout << counter;
}

This prints 1 when I enter a string < 1000, and 0 when enter a string with non-digits or digits >= 1000.

Enter string: 999
1 
Enter string: a123
0 
Enter string: 1234
0 


As someone in the other thread on this topic noted, more work is needed if you want to parse negative numbers in this manner (check if first digit is '-').
Last edited on
My aim is not counting number of digits. It's to count occurrence of number less than 1000 in a string
Oops

1
2
3
4
5
6
7
8
    stringstream in;

    // read input
    string input;
    getline(cin, input);

    // initialize string stream
    stringstream in(input);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <sstream>
#include <string>

int main()
{
   std::cout << "Input a string:\n";
   std::string input { };
   std::getline(std::cin, input);
   std::cout << '\n';

   std::stringstream in { input };
   std::string word { };
   int counter { };

   while (in >> word)
   {
      try
      {
         int temp = std::stoi(word);

         if (temp < 1000) { counter++; }
      }
      catch (...)  // word didn't contain an int
      {
         continue;
      }
   }

   std::cout << "There are " << counter << " numbers less than 1000\n";
}

Input a string:
More like cat 7 dog 42 9000

There are 2 numbers less than 1000
Thanks a lot bro
Sorry,you recently helped me on my program to count numbers found in string.
But now I want to update the program to ask the user how many strings he wants to Input vertically.
Then that will be the limit of the strings which should be entered.

Example
Output:Enter number of strings:
Input:7
Output:Enter strings:
Input:
More
Like
Cat
7
dog
42
9000
Output:There are 2 numbers less than 1000.
Sorry for disturbing and thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main()
{
   cout << "Enter a succession of words, one per line. Finish with a blank line:\n";
   string word;
   int counter = 0;
   while ( getline( cin, word ) && !word.empty() )
   {
      if ( word.find_first_not_of( "0123456789" ) == string::npos && stoi( word ) < 1000 ) counter++;
   }
   cout << "There are " << counter << " numbers less than 1000\n";
}


Enter a succession of words, one per line. Finish with a blank line:
More
like
cat
7
dog
42
9000

There are 2 numbers less than 1000
Thanks... though the user need to decide for himself the limit of words he should enter
How do you finish with a blank line?
Dee5 wrote:
though the user need to decide for himself the limit of words he should enter


Yes, when he/she has finished ... he enters a blank line. Saves counting.

cout << "Enter a succession of words, one per line. Finish with a blank line:\n";


If you want to specify the number of strings then prompt the user for it first, then use a for loop.
Last edited on
How do you finish with a blank line?

The user hits enter/return without typing anything.

while (std::getline(std::cin, word) && !word.empty() )
I do want to specify the number of strings but the problem comes from the for loop
Count<<" Enter number of strings:";
Cin >> n;
For (int i = 0 ; i <n ; ++n)

Is it correct?
No, it is not.

Your syntax is wrong, again. Count? Cin?

Whatever you are using to type your code is doing you NO favors.

And PLEASE learn to use code tags!
Pages: 12