-2

I have a file which looks like this:

2,1,4,6,7
1,2,3,6,5

I have to count the the digits in a single line while ignoring the comma. For example, the first line has 5 digits, and so does the second line. The number of digits can vary in each line. I used getline with a comma delimeter. However, if I do that, I don't know when the line ends. The code I have written will give me the count for whole file. All I want is a way to count the digits in a single line. How do I do that?

numberofdigits = 0;

while(!friendsFile.eof())
{       
    getline(friendsFile,counts,',');
    intcounts = stoi(counts);
    cout << intcounts;
    numberofdigits++;
}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • `while(!friendsFile.eof())` Ouch. [c++ - Why is iostream::eof inside a loop condition considered wrong? - Stack Overflow](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – MikeCAT Mar 03 '19 at 14:20
  • If you want to read a line of input, that's what `std::getline` is for. After you've read one line, then you can figure out how many comma-delimited values it has. – Sam Varshavchik Mar 03 '19 at 14:22
  • I have tried it too. It doesn't helps. @SamVarshavchik – usman siddique Mar 03 '19 at 14:26
  • Of course, it helps. It's very easy to do. Unfortunately, "it doesn't help" is not a useful problem description that anyone else can use to give you further advice. – Sam Varshavchik Mar 03 '19 at 14:39
  • while(!friendsFile.eof()){ getline(friendsFile,counts); intcounts = (stoi(counts.substr(counts.find(',')-1,1))); cout << intcounts; } – usman siddique Mar 03 '19 at 14:43
  • This is what i did. It gives an exception – usman siddique Mar 03 '19 at 14:43
  • Can you explain in detail how i can count the digits in a single line? @SamVarshavchik – usman siddique Mar 03 '19 at 15:01
  • Why, iterate over each character in the line, of course. For each character in the string: is this character a digit? If so, increment the counter. And the end of the line you have your count. Which part of this basic task you're not sure about? – Sam Varshavchik Mar 03 '19 at 15:02
  • How do i get the length of whole line? I used strlen but it gives error. It says "no suitable conversion from string to const char exists " – usman siddique Mar 03 '19 at 15:09
  • 1
    How exactly did you come to a conclusion that `strlen()` is a way to obtain the length of `std::string`? `strlen()` is a C library function that knows nothing about C++ classes. Looks like you you need [to read a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). stackoverflow.com is not designed as a C++ tutorial site. If you want to learn C++, the best way to do so is to read a good book, instead of trying a trial-by-error approach. – Sam Varshavchik Mar 03 '19 at 15:17

1 Answers1

0

Combine your solution with reading line by line, and with a little help of std::stringstream:

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

int main() {
    std::ifstream input{"input.txt"};

    if(!input) {
        std::cerr << "Failed to open the file\n";
        return 1;
    }

    std::size_t line_number = 0;
    std::istringstream stream{};

    for(std::string line{}; std::getline(input, line); stream.clear()) {
        stream.str(line);
        std::size_t count = 0;
        std::string number{};

        while(std::getline(stream, number, ',')) {
            ++count;
        }

        std::cout << "Line " << line_number++ << " contains " << count << " digits\n";
    }
}

After you make sure that the file was opened successfully, you can start processing the file.

First, pay attention to the for() loop. We use a line string to save a line read from the file. The condition of the loop is also the part where we read the line. std::getline not only reads a line, but also returns the stream from where it was reading. That stream can be implicitely convered to bool to check whether the stream is in a valid state. By doing that, we are reading the file line by line and making sure that if something goes wrong (i.e., we reach end of the file), we won't enter the loop and use, potentially corrupted, data. That's a big difference between using this method and !file.eof(), which is almost always wrong.

Then, after reading a line, we initialize an std::istringstream object with it. Stringstreams are useful helpers which enable us to operate on text as though it was a stream. We again use std::getline, but this time to extract all the tokens separated by ','.

The stream.clear() part in necessary after we process a single line, because it clears all bad states of the stream (i.e., the state of reaching end of file - in our case, after reading the whole line).

We count the number of successfull extractions and display it on the screen.

Fureeish
  • 12,533
  • 4
  • 32
  • 62