Finding the hidden numbers

Pages: 12
Hello guys,I'm kinda new in c++ and Im having a little problem. I'm trying to make a program which will count the occurrence of numbers less than 1000 in a list of numbers and words.
Any help will do.
Thanks
Last edited on
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/files/

What have you tried so far?

Note that if you have something like
std::ifstream fin("numbers.txt");

Each number can be extracted like:
1
2
3
4
5
int number;
fin >> number; // number will now contain the number from the file.

fin >> number; // number will now contain the second number from the file.
hint: loop

numbers and words

Note though that fin >> number; will fail and set fin into error state that has to be cleared before next input, if the next "word" is not a number.

A text that has some numbers somewhere is more challenging to read than a well formatted data, where you know when to expect numbers. (Although, well formatted data can still have errors.)
Ah, missed that..
No, it's a list of word and numbers each on it own line..it's not a string
Be exact.
"word and numbers" as in "one word and many numbers" or "one word and one number"?
It's words and numbers
Like in
"cat dog badger 7 42"
or
"cat 7 dog 42 badger 9000"
Read the stream successively into a string until the stream dies.

For each string use string.find_first_not_of to determine whether it is all digits.

For any string that is purely digits, increment count if its length is less than 4.


It gets a tad more difficult if you allow negative numbers, floating point or any other acceptable representation of "number".

You may need to be more explicit about your requirements.
More like cat 7 dog 42 9000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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";
}


There are 2 numbers less than 1000
Thanks so much bro
It gives an error which says "use of undeclared identifier 'stoi'
Also the program needs to take input from a user
Dee5 wrote:
It gives an error which says "use of undeclared identifier 'stoi'

Time to upgrade your compiler to at least C++11.

The following would have the same effect here, as long as none of your numbers start with 0.
if ( word.find_first_not_of( "0123456789" ) == string::npos && word.size() < 4 ) counter++;



Dee5 wrote:
Also the program needs to take input from a user

You can change the while loop to run off any sequence of words. Only you know how you wish to take the input from the user:
- whole line?
- multiple lines?
- sequence of words ended by a sentinel.

Over to you.
Multiple lines sir
Multiple lines...
Dee5 wrote:
Multiple lines...

How do you propose to signal the end of those "multiple lines"?
Limit of 10 lines sir
Pages: 12