-1

How do I read the new line character? I am trying to do a character count, but the new line gets in the way. I tried doing if (text[i] == ' ' && text[i] == '\n') but that didn't work. Here is my repl.it session.

I am trying to read this from file.txt:

i like cats
dogs are also cool
so are orangutans

This is my code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream input;
    input.open("file.txt");

    int numOfWords = 0;

    while (true)
    {
        string text;
        getline(input, text);

        for(int i = 0; i < text.length(); i++)
        {
            if (text[i] == ' ') 
            {
                numOfWords++;
            }
        }

        if (input.fail())
        {
            break;
        }
    }
    cout << "Number of words: " << numOfWords+1 << endl;
    input.close();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hui Yang
  • 1
  • 1
  • 2
  • Good start. You almost avoid the [while !eof bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), but `if (input.fail())` is in the wrong spot. Move it up to before you start parsing a line you may not have successfully read or give `string text; while (getline(input, text)) { for (...) { ... } }` a try. This reads a line into text or exits the loop all in one shot. – user4581301 Dec 12 '17 at 03:10
  • 2
    The code calls `std::getline`, which swallows the newline. Just pretend that the string you read has a newline at the end. – Pete Becker Dec 12 '17 at 03:11
  • @PeteBecker: true, except that you don't know if the file uses `\n` or `\r\n` for the line breaks. In the latter case, should a line break be counted as 1 or 2 chars? The OP needs to decide on that. On the other hand, the code is only counting words, not characters. But the question is asking how to count characters, not words. The OP needs to clarify the requirements – Remy Lebeau Dec 12 '17 at 03:23
  • @RemyLebeau -- in C and C++ `\n' is the new line character. The standard library does whatever is needed to match the encoding used in native files. If you have foreign files on your system you need to modify them to satisfy the OS's file-ending conventions. That's a simple filter or, if you're using FTP to transfer files, FTP can do that for you. Sure, you can add all kinds of workarounds to bypass the design of the stanard library, but the **point** of having a standard library is that you shouldn't need to do that. – Pete Becker Dec 12 '17 at 13:36

1 Answers1

1

Your question is asking how to count characters, but your code is counting words instead. std::getline() swallows line breaks. You don't need to worry about them if you want to count words. In fact, you can use operator>> to greatly simplify your counting in that case, eg:

int main()
{
    ifstream input("file.txt");

    int numOfWords = 0;
    string word;

    while (input >> word)
        ++numOfWords;

    cout << "Number of words: " << numOfWords << endl;

    return 0;
}

If you really want to count characters instead of words, use std::ifstream::get() to read the file 1 character at a time, eg:

int main()
{
    ifstream input("file.txt");

    int numOfChars = 0;
    int numOfWords = 0;
    bool isInSpace = true;
    char ch;

    while (input.get(ch))
    {
        ++numOfChars;

        if (std::isspace(ch, input.getloc())) {
            isInSpace = true;
        }
        else if (isInSpace) {
            isInSpace = false;
            ++numOfWords;
        }
    }

    cout << "Number of chars: " << numOfChars << endl;
    cout << "Number of words: " << numOfWords << endl;

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770