Cannot count words properly - File Handling Error

Hey Guys I am making a program to revise file handling,i thought i was pretty much done with it for now so thought to a make a huge program which will cover all the things reading,writing,pointer placing etc whatever i've learned but i am stuck at the case 3 of this program. If i enter the word as "He" for example,it does not counts in properly if it's 1 time appearing it will show 3 times or something idk , i tried specifying string length also but it just didn't work,Please Help So I can expand this program further.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std ;
int main()
{
    string content ; int choice , _count = 0 , exit_program ;
    char ch , letter  , word[30] , word_to_count[30] ;
    fstream f1 ;
    f1.open("DATA.TXT"  , ios :: out) ;
    cout<<"\nEnter The Content For The File : " ;
    getline(cin,content) ;
    f1 << content ;
    f1.close() ;
    cout<<"\tPress 0 To Use MENU AGAIN AND 1 TO EXIT THE PROGRAM!!" ;
    system("cls") ;
    do
    {

        cout<<"\t\nMENU - : \n1.Count Occurrence Of A Single Letter\n2.Count Number Of Vowels \n3.Count Number Of Times a word appears \n4. Substitute A Character(ON SCREEN ONLY,OPTION 8 FOR FILE FORMATTING)" ;
        cin>>choice;

    {
            switch(choice)
    {
        case 1 : f1.open("DATA.TXT" , ios :: in) ;
        cout << "\nEnter The Letter You Want To Count " ;
        cin >> letter ;
        while(f1.get(ch))
        {
            if (letter == ch)
                _count++;
        }
        cout<<"\nThe Number OF Times " << "'" << letter << "' appears is = " <<_count ;
        f1.close() ;
        cout<"\n" ;
        cin>>exit_program ;
        break ;


        case 2 : f1.open("DATA.TXT" , ios :: in) ;
        while(f1.get(ch))
        {
            if ((ch== 'a' || ch == 'A' || ch == 'e' || ch =='E' || ch == 'o' || ch == 'O' || ch == 'I' || ch == 'i' || ch == 'u' || ch == 'U'))
                _count++;
        }
        cout<<"\nNumber Of VOWELS IN THE FILE ARE : "  << _count ;
        f1.close() ;
        cout<"\n";
        cin>>exit_program ;
        break ;

        case 3 : f1.open("DATA.TXT" , ios :: in) ;
        cin.ignore() ;
        cout << "\nEnter The WORD Whose Occurrence You Want To Count : " ;
        gets(word_to_count) ;
                while(f1 >> word)
        {
            if(strcmp(word_to_count,word) )
                _count++;
        }
        cout << "\nNumber Of Times " << word_to_count << "Appears is = " << _count ;
        f1.close() ;
        cout<"\n" ;
        cin>>exit_program ;
        break ;
        case 4 : char to_replace , to_be_replaced ;
        f1.open("DATA.TXT" , ios :: in ) ;
        cout<<"\nEnter The Letter You Want To REPLACE : " ;
        cin>>to_replace ;
        cout<<"\nEnter The Letter To Be Replaced : " ;
        cin>>to_be_replaced ;
        while(f1.get(ch))
        {
            if (ch == to_replace)
                ch = to_be_replaced ;
                cout<< ch ;

        }
        while(f1.get(ch))
        {
            cout << ch ;
        }
        cout<"\n" ;
        cin>>exit_program ;
        f1.close() ;

    }

    }
    } while (exit_program == 0) ;

    return 0 ;
}



Why this horrible mix of C and C++ ?
Here is a quick and dirty demo - error handling omitted for brevity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<fstream>
#include <string>

using namespace std;

int main()
{
  string input;
  ofstream dest("words.txt");
  cout << "Enter some text. ";
  getline(cin,input);
  dest << input;
  dest.close();

  ifstream src("words.txt");
  string search_word;
  cout << "Enter word to count. ";
  getline(cin,search_word);
  int count = 0;

  while (src >> input)
  {
    if (input == search_word)
      count++;
  }

  cout << "The word '" << search_word << "' occured " 
       << count << " times" << "\n";

  return 0;
}
Well I agree it might be a mix but how do i ignore case without using C-String library? i am not quite versed with that yet,getting there slowly.

Can you just tell me what is the error in mine code? I appreciate your help really but just correct mine code , case 3 particularly?
Well I agree it might be a mix but how do i ignore case without using C-String library

He's probably most concerned about your use of gets, which is obsolete even in C.
what's the best alternative then? cin,getline() ????????/
Your code is too hard to read for me. The spacing is freaky. Nobody puts a space before the semicolon or spaces around ::. It's massive overkill. And at the same time you you're not putting spaces around << and scrunching things onto the same line and not indenting consistently. It hurts my eyes.
Last edited on
You could be just a little bit less offensive in the light that i am just a goddamn beginner,trying to learn this language on my own. You could have just told me to what to correct, how to write code properly if you ain't got advice better don't goddamn reply to threads. I would have happily taken the advice but discouraging someone is mean and brings morale down.
Topic archived. No new replies allowed.