Thread: how do i do this?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    how do i do this?

    Hi, everyone. I'm completely new to the forum and C++.
    This is my forth program for the c++ programming class. I don't have a clue what i should do to get my hand on this program.
    here it is:
    _________
    You are to write a C++ program that will scan a text file and do some elementary analysis that might indicate how difficult the text is to read. In particular, your program will:
    1. Determine the average length of all the words in the text
    2. Find the longest word in the text and display it
    3. Determin the average number of words in each sentence
    4. Find the longest sentence and display it

    "Mr." is not an actual word.
    You are allowed to use while, if , else condition only, no For condition
    _________
    This is what I got so far, I don't know what I should do next.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	
    	string fileName;
    	string getout;
    	string words;
    	int word_count;
    	string space = " ";
    
    	ifstream infile;
        
    	bool run = true;
    	
    	while(run)
    	{
    		cout << "Please insert the file name: ";
    		cin >> fileName;
    		infile.open(fileName.c_str());
    
    		if (infile.fail())
    		{
    			cout << fileName << " cannot be found." << endl;
    			cout << "Please try again, do you want to quit? (y/n)";
    				
    				cin >> getout;
    				if (getout == "y")
    				{
    					cout << "You decide to stop the program \n";
    					return 0;
    				}
    			
    		}
    		else 
    		{
    			while (run)
    			{
    				cin words;
    				____help___Please____
    			}
    
    		}
    	
    	
    	}
    return 0;
    }
    Thanks,

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    wrong forum.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    oops, I'm sorry. I just finished reading the forum faqs. sorry about that, and thanks anyway.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question Wrong forum? Why?

    'Cause he/she's asking for homework help??? It IS OK to ask for help as long as you're not asking someone to write the program for you.

    silvercon, if you're still around... Check the characters as they are read-in from the file. Just look for spaces, and count the (non-space) characters between spaces. Look for periods and count the words between periods. You're going to have to read the words (between spaces) into a string (or character array) and look for "Mr." too.

    For example if x = ' ' then you've found a space. if x = '.' then you've found the end of a sentence. The single-quotes allow you to get the ASCII value of a single character.

    Of course, you're going to need variables for the length and number of words, etc.

    I didn't study the code you have already, and I'm not at my compiler, so I can't help with the details.

    [EDIT]:
    >>Wrong forum? Why?

    because it was in GD before it was moved.
    Ooohhh!! Sorry I questioned your post RoD!
    Last edited by DougDbug; 02-13-2003 at 01:10 PM.

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>Wrong forum? Why?

    because it was in GD before it was moved.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Thanks, DougDbug. I think I understand how to do it now. That's all I want, the idea of how to get my hand on this program. Thanks again.

Popular pages Recent additions subscribe to a feed