Counting a Specific word from a Text File in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Counting a Specific word from a Text File in C++

  1. #1
    Join Date
    Nov 2012
    Posts
    6

    Counting a Specific word from a Text File in C++

    Hi. I am a beginner in C++. I have this assignment. I've spent about a whole day completing this assignment but i don't know how to do that.

    Here is my assignment question:

    The user will first enter the file name to be processed. The program can do the following 5 tasks:
    1. Count the number of words
    2. Count the number of alphabets (without punctuation marks)
    3. Count number of sentences.
    4. Count the frequency of each vowel.
    5. Count frequency of the following three words individually: FAST, computer and engineering
    There should be a proper menu through which the user can select the desired task. The program should
    continue until the user asks to terminate it.

    Here is what i have done so far:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    #include <cstdlib>

    using namespace std;



    int main(void)
    {
    int opt=0; //option number
    ifstream fin;
    string filename;

    cout << "Welcome\n" << endl;
    cout << "This programe will analyze the file content &" << endl;
    cout << "compute the contents you want of the file you input.\n\n\n\n\n" << endl;
    system("pause");
    system("cls");

    //Getting the input file and openning it
    cout << "Enter input data file name:" << endl;
    cin >> filename;
    cout << endl;

    fin.open(filename.c_str()); // Convert to C-string and open it.

    if (!fin) // Will fail if file didn't exist.
    {
    cout << "Unable to open " << filename << endl;
    cin.get();
    system("cls");
    }

    while (opt != 'q')
    {
    cout << "Choose one of the following tasks (1-5): " << endl << endl;
    cout << "1- Count the number of words" << endl;
    cout << "2- Count the number of alphabets (without punctuation marks)." << endl;
    cout << "3- Count the number of sentences" << endl;
    cout << "4- Count the frequency of each vowel" << endl;
    cout << "5- Count frequency of the following three words individually: FAST, computer" << endl;
    cout << " and engineering" << endl;
    cout << "To quit, enter q" << endl;

    cin >> opt;
    cout << endl;



    char next;
    int alphabets = 0;
    int words = 0;
    int sentences = 0;
    int vowel_a = 0;
    int vowel_e = 0;
    int vowel_i = 0;
    int vowel_o = 0;
    int vowel_u = 0;
    int f = 0; //fast
    int comp = 0; //computer
    int eng = 0; //engineering


    if (opt == 1)
    {
    while((next = fin.get()) != EOF)
    {
    if(isspace(next)) //calculate total numbers of words
    words++;
    }

    cout << "Total Number of Words: " << words << endl << endl;
    }

    if (opt == 2)
    {
    while((next = fin.get()) != EOF)
    {
    if(isalpha(next)) //calculate total numbers of alphabets
    alphabets++;
    }

    cout << "Total Number of Alphabets: " << alphabets << endl << endl;
    }

    if (opt == 3)
    {
    while((next = fin.get()) != EOF)
    {
    if(next == '.' || next == '!' || next == '?') //calculate total numbers of sentences
    sentences++;
    }

    cout << "Total Number of Sentences: " << sentences << endl << endl;
    }

    if (opt == 4)
    {
    while((next = fin.get()) != EOF)
    {
    if(next == 'a' || next == 'A') //calculate total numbers of 'a' alphabets
    vowel_a++;

    if(next == 'e' || next == 'E') //calculate total numbers of 'e' alphabets
    vowel_e++;

    if(next == 'i' || next == 'I') //calculate total numbers of 'i' alphabets
    vowel_i++;

    if(next == 'o' || next == 'O') //calculate total numbers of 'o' alphabets
    vowel_o++;

    if(next == 'u' || next == 'U') //calculate total numbers of 'u' alphabets
    vowel_u++;


    }

    cout << "Total Number of vowel 'a': " << vowel_a << endl;
    cout << "Total Number of vowel 'e': " << vowel_e << endl;
    cout << "Total Number of vowel 'i': " << vowel_i << endl;
    cout << "Total Number of vowel 'o': " << vowel_o << endl;
    cout << "Total Number of vowel 'u': " << vowel_u << endl;
    }

    if (opt == 5)
    {
    while ((fin.get ()) != EOF)
    {
    string word [50];

    for (int i=0; i<50; i++)
    fin >> word[i];

    for (int i=0; i<50; i++)
    {
    //cout << word [i] << endl;
    /*if (word[i] == 'FAST')
    f++;

    if (word [i] == 'computer')
    comp++

    if (word [i] == 'engineering')
    eng++*/
    }

    cout << "The word FAST is repeated " << f << " times" << endl;
    cout << "The word computer is repeated " << comp << " times" << endl;
    cout << "The word engineering is repeated " << eng << " times" << endl << endl;
    }
    }
    }
    return 0;
    }



    Sorry the code is long.

    There are a few problems.
    1- When I enter option 'q', an infinite loop starts.
    2- The first time, the program gives the correct value. After that, each time it gives answer ZERO.
    3- The last part is not working. I experimented something which failed. I don't know how to do the last part.

    i would really appreciate any kind of help. I don't want to get another F in programming assignment! :/

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,360

    Re: Counting a Specific word from a Text File in C++

    First you should use Code tags to make your code readable and understandable. Otherwise noone will try to analyze it.
    Second, You have to debug your code to see/understand what and where and why goes wrong. Did you fry to debug?
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center