CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2011
    Posts
    0

    Miscalculating words/spaces

    I was suppose to create a programs that reads out all characters, spaces, and words in a txt file, also a word would be anything followed by a space and also includes punctuation marks as words.
    So far I have done everything that I am suppose to, but running the txt file under certain circumstances such as "space.space" in creates double words this is my code so far.

    Code:
    #include <fstream>
    #include <iostream>
    #include <conio.h>
    #include <cctype>
    using namespace std;
    
    
    int main()
    {
    	char ch,oldch;
    	int cnt_ch=0, cnt_words=0, spacenum=0;
    
    
    	ifstream poemfile;
    
    	poemfile.open("F:\\Super Computer programming\\Roses.txt");
    
    
    	if( poemfile.fail() )
    	{
    		cout<<"The File could not be found. Terminating\n";
    		getch();
    		exit(1);
    	}
    
    
    
    
    	ch = poemfile.get();
    	
    	
    	
    
    	while( !poemfile.eof() )   
    	{   
    		cout<<ch; 
    		cnt_ch++;
    		ch=poemfile.get();
    
         if(ch == ' ')
    	{
    		spacenum++;
        }
        
         if ((ch == ' ') && (oldch != ' ') ) cnt_words ++;  
     
      
        	     if ((ch == '?') || (ch == '.') || (ch == '!')) 
                    {
        
                          cnt_words++;
           
                    }
       
    
    }
    
    	poemfile.close();
    
    	cout<<"\nThe file had "<<cnt_ch
    		<<" characters, "<<spacenum
    		<<" spaces, and "<<cnt_words+1
    		<<" words\n";
    
    	cout<<"\n\nNormal termination.\n";
    	getch();
    	return 0;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Miscalculating words/spaces

    This code
    Quote Originally Posted by raulian View Post
    Code:
    if ((ch == '?') || (ch == '.') || (ch == '!')) 
    {
        cnt_words++;
    }
    contradicts the statement
    Quote Originally Posted by raulian View Post
    ...a word would be anything followed by a space and also includes punctuation marks as words.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Miscalculating words/spaces

    Indentation shouldn't be random. Your code would be a lot easier to read and debug if it were formatted better.

    You're checking for the value in oldch, but you're never setting it anywhere.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured