0

I need help reading the commas and periods as spaces in a .txt file. The program runs smoothly and outputs the correct number of words and numbers, but I need the numbers that have periods and commas to read as separate numbers.

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word)            // read the text file word-by-word
    {
        if (isdigit(word.at(0)))    // if the first letter a digit it counts as number
        {
            countNumbers++;
        }
        else
        {
            countWords++;
        }
        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}
90s_Kidd
  • 31
  • 1
  • 7

2 Answers2

2

Instead of reading word by word, you could read all the line and iterate over it character by character. So, when you find a whitespace, dot or comma, you can check if it's a word or number.

So, you can use the function getline to read a line and iterate over all chars. The code would look like this:

while (getline(file, line))
{
    string currentWord;
    char currentChar;
    for (int i = 0; i < line.length(); i++)
    {
        currentChar = line[i];
        if (currentChar == ' ' || currentChar == ',' || currentChar == '.')
        {
            if (currentWord != "")
            {
                // check if currentWord is composed by letters or numbers here
            }
            currentWord = "";
        }
        else
        {
            currentWord += currentChar;
        }
    }
}

Maybe you'll also need to check if currentChar is different of '\r' or '\n'. Another option is to check if currentChar is neither a letter or a digit.

Also, remember to close your file after reading it.

1

Here is a solution to your newly modified problem. The approach I took was to walk through each string and count either numbers or words, taking into account that periods and commas should be treated as spaces. As in your previous question, I assume that a given word cannot consist of a combination of letters or numbers. In other words, if the first character be a number, then entire word is a number, or else it is a character word.

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void countWord(const string& word, int &countNumbers, int &countWords);

int main(int argc, char* argv[]) {
    int countWords = 0, countNumbers = 0;

    ifstream file;
    file.open ("input1.txt");
    string word;

    while (file >> word) {
        // iterate over each "word"
        countWord(word, countNumbers, countWords);

        cout << word << " ";
    }

    cout << endl;
    cout << "Words = " << countWords << "      Numbers = " << countNumbers << endl;

    system("pause");
    return 0;
}

void countWord(const string &word, int &countNumbers, int &countWords) {
    bool word_start = true;

    for (int i=0; i < word.length(); i++) {
        if (word_start == true) {
            if (word.at(i) == '.' || word.at(i) == ',') {
                continue;
            }
            else if (isdigit(word.at(i)) {
                countNumbers++;
            }
            else {
                countWords++;
            }
            word_start = false;
        }

        if (word.at(i) == '.' || word.at(i) == ',') {
            word_start = true;
        }
    }

    return;
}
Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I keep getting the following errors in VS2010. – 90s_Kidd Sep 01 '15 at 18:33
  • Yeah it was 13, but when I tried removing the '&' sign for the void function the count was 0. Im sorry im just so confused and dont know what to do – 90s_Kidd Sep 02 '15 at 01:44
  • Use my code as is. I think you are not actually using my code, though. I had `boolean` as a type, which is wrong, and should have caused a compilation error. Use my code exactly as I have written it, and then come back here with the output. Try using this _single_ line input: `10 20 3.1416,,1000` – Tim Biegeleisen Sep 02 '15 at 01:57
  • I found the problem ! – 90s_Kidd Sep 02 '15 at 02:28
  • I have to use 'if (word_start == true)' instead of 'if (word_start =true)' – 90s_Kidd Sep 02 '15 at 02:37
  • Brilliant...I can't believe I missed this. I don't have a C++ compiler, so I was unable to test the code. – Tim Biegeleisen Sep 02 '15 at 02:39