Finding the hidden numbers

Pages: 12
Dec 8, 2019 at 3:06pm
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 Dec 8, 2019 at 3:11pm
Dec 8, 2019 at 3:24pm
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.
Dec 8, 2019 at 3:30pm
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

Dec 8, 2019 at 3:40pm
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.)
Dec 8, 2019 at 4:59pm
Ah, missed that..
Dec 8, 2019 at 5:03pm
No, it's a list of word and numbers each on it own line..it's not a string
Dec 8, 2019 at 8:02pm
Be exact.
"word and numbers" as in "one word and many numbers" or "one word and one number"?
Dec 9, 2019 at 5:13am
It's words and numbers
Dec 9, 2019 at 7:15am
Like in
"cat dog badger 7 42"
or
"cat 7 dog 42 badger 9000"
Dec 9, 2019 at 7:41am
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.
Dec 9, 2019 at 8:00am
More like cat 7 dog 42 9000
Dec 9, 2019 at 8:13am
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
Dec 9, 2019 at 8:25am
Thanks so much bro
Dec 9, 2019 at 8:37am
It gives an error which says "use of undeclared identifier 'stoi'
Dec 9, 2019 at 8:42am
Also the program needs to take input from a user
Dec 9, 2019 at 8:52am
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.
Dec 9, 2019 at 9:04am
Multiple lines sir
Dec 9, 2019 at 9:08am
Multiple lines...
Dec 9, 2019 at 9:23am
Dee5 wrote:
Multiple lines...

How do you propose to signal the end of those "multiple lines"?
Dec 9, 2019 at 9:45am
Limit of 10 lines sir
Pages: 12