Coding Troubles

This is a discussion on Coding Troubles within the C++ Programming forums, part of the General Programming Boards category; Okay I have code that is supposed to get a number of words in a text file....for some reason it ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    15

    Coding Troubles

    Okay I have code that is supposed to get a number of words in a text file....for some reason it will half of the number of words in the text file....There are actually 6338 words in this text file...I only come up with exactly half which is 3169...I don't see anything wrong with the code so maybe you guys can figure it out

    Code:
    void CountNumberOfWordsAndNumberOfCharacter()
    //Pre:Get Number of Words and Number of characters in Text file
    //Post:Output Number of Words and Number of Characters
    {
    	int Choice = 0;
    	apstring Words;
    	long int TotalWords = 0;
    	char Characters;
    	long int TotalCharacters = 0;
    
    	cout << endl;
    	cout << "Enter your choice count number of words and characters" << endl;
    	cout << endl;
    	cout << "[1] Words.txt" << endl << endl;
    	cout << "[2] melville.txt" << endl << endl;
    	cout << "[3] caesar.txt" << endl << endl;
    	cin >> Choice;
    
    	while(Choice != 1 && Choice != 2 && Choice != 3)
    	{
    		cout << "Input Error, please choose again." << endl;
    		cin >> Choice;
    	}
    
    	switch(Choice)
    	{
    	case 1:
    		{
    			ifstream instream("C:\\STR\\words.txt");
    			while(instream>>Words)
    			{
    				TotalWords++;
    			}
    			while(instream>>Characters)
    			{
    				TotalCharacters++;
    			}
    
    			cout << "Total Amount Of Words : " << TotalWords << endl;
    			cout << "Total Amount of Characters : " << TotalCharacters << endl;
    
    			break;
    		}
    	}
    	
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    Well the characters are a problem just one I am not dealing with right now...but the words for some reason is only half of what it should be and I can't find any logic problems with the code....yeah help with the characters if you want cause I'll do that afterwards..right now words are my main concern

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    Okay I figured out the word thing...I used MS word to count the words and yeah that was wrong...yeah so I used someone elses code to see how many words were there and I was right...now on to the characters....can someone tell me how I can read the file again from the begining cause it seems that the program is not reading the characters...I am assuming it is at the end of the file so how can I bring it back to the begining of the file?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    BTW I am a newbie C++ programmer if you can see....so I don't know too mcuh about the language.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    15
    Never mind I think I figured out the character issue by just declaring another thing called

    Code:
    ifstream instreamTwo("C:\\STR\\melville.txt");
    right before the charcter loop so it should restart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 01:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21