7 Replies - 597 Views - Last Post: 01 March 2016 - 05:49 PM Rate Topic: -----

#1 wyming  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 28-February 16

Program that prints out the number of words in textfiles[help]

Posted 28 February 2016 - 11:12 PM

Hello, so I am writing a program that displays the number of words from textfiles

This is what I have so far
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;




int main () {
    
    
    ifstream inFile;
    string fileName;
    string quitProg;
    int count= 0;

    
    
    
    cout << "Enter name of the file:" ;
    cin >> fileName;
    
    inFile.open(fileName.c_str());
    
    
    while (fileName != "quit" ) {
        
        
        
        inFile >> fileName;
        ++count;
        cout << fileName.length() << endl;
        
        //Porgram asks again if 'quit' is not entered.
        
        cout << "Enter name of the file:" ;
        cin >> fileName;
        
    }
    // If user types "quit", program will stop.
    
    if (fileName == "quit") {
        
        cout << "The program has quit." << endl;
    }
    
    
    inFile.close();
    
    
}







A texfile that my program has to read is this

This &%file that has!!,...

'

This file must have several spaces at the end of the file.
19 words.


So the program is supposed to count "19" for the words on the file, but in only counts the 4 words from the first line. It must count all the words, from all the lines , but I am stuck on that part. please help!

Is This A Good Question/Topic? 0
  • +

Replies To: Program that prints out the number of words in textfiles[help]

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,661
  • Joined: 19-February 09

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 07:02 PM

Hi, four for the first line is correct.
Was This Post Helpful? 0
  • +
  • -

#3 wyming  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 28-February 16

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 08:00 PM

View Post#define, on 29 February 2016 - 07:02 PM, said:

Hi, four for the first line is correct.


Yea but I need the program to read the words from the entire textfile.. any suggestions??
Was This Post Helpful? 0
  • +
  • -

#4 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 224
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 08:32 PM

Where is it counting any words at all? The 4 that it's printing is just the length of the first word on the first line, "This". That's the only thing that you're inputting from the file. You need a loop to keep reading (and counting) words from the file until you reach the end of the file.

Since we can't write that for you, I suggest you study a tutorial on file i/o.
Was This Post Helpful? 1
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,661
  • Joined: 19-February 09

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 08:49 PM

Using the >> operator to a string should work. You are reading the words in the file to to a string called filename. Why not name that string word?

inFile >> word;



Beginners often use eof to check for the end of the file.


while( !inFile.eof() )
{
  //...
}



Although it doesn't always give the desired result.
Was This Post Helpful? 0
  • +
  • -

#6 wyming  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 28-February 16

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 10:15 PM

Ok so I got to where it counts the words of the whole file!.. but just once... Im trying to get this done using a loop that keeps asking the name of a file until the user types "quit",

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




int main () {
    
    
    string fileName;
    int count= 0;
    ifstream inFile(fileName);
    
    
    
    

    cout << "Enter Name of the file:";
    cin >> fileName;
    
    
    inFile.open(fileName.c_str());
    
    
    while (inFile >> fileName) {
        
        count++;
        
    }
    
   
    cout << "The number of words inf ile is:" << count << endl;
    
    
    
}



Im trying to put the COUT statement showing the number of words in file before it asks for another file name,
but if I put it inside the WHILE statement it gives me "1" as the number of words. this is the code I have in mind but it does not work

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




int main () {
    
    
    string fileName;

    int count= 0;
    ifstream inFile(fileName);
    
    
    
    

    cout << "Enter Name of the file:";
    cin >> fileName;
    
    
    inFile.open(fileName.c_str());
    
    
    while (fileName != "quit") {
        
        
        if (inFile >> fileName) {
        
        count++;
        }
    // I think the cout statment should go her to show the output before asking for anotherfile
   
        cout << "The number of words inf ile is:" << count << endl;
       
    
        cout << "Enter Name of the file:";
        cin >> fileName;
        
    }
    
    if (fileName == "quit") {
    
      cout << "The Program has quit." << endl;
    
    }
       
    
}


Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,661
  • Joined: 19-February 09

Re: Program that prints out the number of words in textfiles[help]

Posted 29 February 2016 - 10:51 PM

Loops do multiples of the same actions. You are reading and counting words of a file in one loop. You also want to ask for a filename and open the file and count the words. You need another loop, one of which will be in the other (nested).
Was This Post Helpful? 1
  • +
  • -

#8 wyming  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 28-February 16

Re: Program that prints out the number of words in textfiles[help]

Posted 01 March 2016 - 05:49 PM

Great that was a nice tip!.. I think this would be my last issue with the program: After it counts the words of the first file, I try to count the words of the next file, and it displays the same number of words from the previous file. I think is just storing the value from the first file, how can I make it forget the count of words of the previous file? This is my code:

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

int main () {
    
    
    string fileName;
    int count= 0;
    ifstream inFile(fileName);
    
    
    
    cout << "Enter name of the file:" ;
    cin >> fileName;
    inFile.open(fileName.c_str());
    
  
    while (fileName != "quit"){
        
        while (inFile >> fileName ) {
            count++;
          
        }
       cout << count << endl;
     
      
        
       
        
        cout << "Enter name of the file:" ;
        cin >> fileName;
        inFile.open(fileName.c_str());


    }
    }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1