1

I program which reads the letters and numbers from the input being used. But i dont know how to implement this to a .txt file. This is my code:

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

    int main()
    {
      char ch;
        int countLetters = 0, countDigits = 0;

        cout << "Enter a line of text: ";
        cin.get(ch);

        while(ch != '\n'){
            if(isalpha(ch))
                countLetters++;
            else if(isdigit(ch))
                countDigits++;
            ch = toupper(ch);
            cout << ch;
            //get next character
            cin.get(ch);
        }

        cout << endl;
        cout << "Letters = " << countLetters << "      Digits = " << countDigits << endl;

        return 0;
    }

I made a mistake in my HW I was suppose to count the words instead of letters from the .txt file. Im having trouble counting words because I get confused with the space between words. How could I change this code to count the words instead of letters? I really appreciate the help.

therainmaker
  • 4,253
  • 1
  • 22
  • 41
90s_Kidd
  • 31
  • 1
  • 7
  • do a web search for "C++ read text file". try to do it yourself, and come back here in case of specific problems. – hoijui Aug 31 '15 at 05:01

2 Answers2

1

This code counts each word separately. If the first character of a "word" is a number, it assumes the entire word is numeric.

#include <iterator>
#include <fstream>
#include <iostream>

int main() {
    int countWords = 0, countDigits = 0;

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

    while (file >> word) {        // read the text file word-by-word
        if (isdigit(word.at(0)) {
            ++countDigits;
        }
        else {
            ++countWords;
        }
        cout << word << " ";
    }

    cout << endl;
    cout << "Letters = " << countLetters << "      Digits = " << countDigits << endl;

    return 0;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Will I have to use the same method to count the numbers? Currently this method counts the numbers as words also. – 90s_Kidd Aug 31 '15 at 06:08
  • Are you sure you won't have any words which mix numbers _and_ letters? Yes, this currently counts each word, be it "number" or otherwise. – Tim Biegeleisen Aug 31 '15 at 06:09
  • I have to count words and numbers seperately this is where I get stuck. I got confused thinking I had to count the individual letters and digits. Our professor said we wont be required to read a combination of letters such as X2 or student123. But instead: The cat is [there]\n 10 20 3.1416,,1000\n (all this is in the .txt file) – 90s_Kidd Aug 31 '15 at 06:17
  • I assume a word is a number if the _first_ letter is a number. Maybe this will be enough for your problem. Check out my updated answer. – Tim Biegeleisen Aug 31 '15 at 06:18
  • I got new instructions from my professor on the project you helped me with. Would you mind helping me out? I would really appreciated. I posted the question under a new title: Reading ' , ' and ' . ' as spaces in a .txt file. – 90s_Kidd Sep 01 '15 at 04:32
0
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  char ch;
    int countLetters = 0, countDigits = 0;

    ifstream is("a.txt");

    while (is.get(ch)){
        if(isalpha(ch))
            countLetters++;
        else if(isdigit(ch))
            countDigits++;
        ch = toupper(ch);
        cout << ch;
    }

    is.close();

    cout << endl;
    cout << "Letters = " << countLetters << "      Digits = " << countDigits << endl;

    return 0;
}
tepl
  • 411
  • 3
  • 4
  • Thank you so much this helped a lot!!! Would you mind answering something else? My text file has 3 line which are: The cat is [there]\n 10 20 3.1416,,1000\n another cat\n It reads all the letters and numbers perfectly, but how would I read the words instead of letters? I made a mistake in my HW it asks to count the words not letters. – 90s_Kidd Aug 31 '15 at 05:15