Thread: Maybe you can help..loop problem possibly?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    Maybe you can help..loop problem possibly?

    I wrote this program that takes a text file, inputs it one character at a time. The program counts the number of lines, words, and paragraphs, then outputs them. As the program does this, it also outputs the input file to output.txt without spaces. Problem is, when i run it, it just sits there and does nothing. Been looking at it for an hour, cant figure out why. I know this is extremely vague, but if you have ideas thatd be great.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    //prototyping
    void initialize(int& words, int& lines, int& paragraphs, char& letter, int& totalWords);
    void processBlank(ofstream& output, char& letter, int& words);
    void copyText(ofstream& output, char& letter);
    void updateCount(int& words, int& lines, int& paragraphs, int& totalWords);
    void printTotal(int& lines, int& paragraphs, int& totalWords);
    
    
    
    int main()
    {
    	int words, lines, paragraphs, totalWords;
    	char letter;
    	initialize(words, lines, paragraphs, letter, totalWords);
    	ifstream input ("Ch7_Ex7Data.txt");
    	if (!input)
    	{
    		cerr << "The system cannot open the input file.";
    		exit(1);
    	}
    	
    	ofstream output ("output.txt");
    	if (!output)
    	{
    		cerr << "The system cannot open the output file.";
    		exit(1);
    	}
    	
    	letter = input.get();
    		
    	while (!input.eof())
    	{          
              while (letter != '\n')
              {
                    processBlank(output, letter, words);
                    copyText(output, letter);
              }
              
              updateCount(words, lines, paragraphs, totalWords);
              
              letter = input.get();
          
        }
        
     
            
        //closing my files
    	input.close();
    	output.close();
    
        printTotal(lines,paragraphs,totalWords);
    
    	return 0;
    }
    
    
    void initialize(int& words, int& lines, int& paragraphs, char& letter, int& totalWords)
    {
    	words = 0;
    	lines = 0;
    	paragraphs = 0;
    	letter = ' ';
    	totalWords = 0;
    }
    
    void processBlank(ofstream& output, char& letter, int& words)
    {
    	if (letter != ' ')
    	{
           words++;
        }
    }
    
    void copyText(ofstream& output, char& letter)
    {
         if (letter != ' ')
         output.put(letter);
    }
    
    void updateCount(int& words, int& lines, int& paragraphs, int& totalWords)
    {
         if (words = 0)
         {
             paragraphs++;
         }
         
         words += totalWords;
         
         if (words != 0)
         {
             lines++;
         }
         
         words = 0;
                 	
    }
    
    void printTotal(int& lines, int& paragraphs, int& totalWords)
    {
         cout << "Number of words: " << totalWords;
         cout << "Number of lines: " << lines;
         cout << "Number of paragraphs: " << paragraphs;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If the program does nothing, you debug it

    Learn to use the debugger. It's the coolest thing after coding. You place brakpoints at certain spots, step through the program line by line and you will soon find what is wrong.

    Code:
    void initialize(int& words, int& lines, int& paragraphs, char& letter, int& totalWords)
    This is completely unecessary. It's a big ugh! Just initialize the variables at the point of declaration. You will thank yourself later when rereading your code.


    Code:
    	letter = input.get();
    		
    	while (!input.eof())
    	{          
              while (letter != '\n')
    look closely at that part of your code... what do you think happens on the while loop?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Quote Originally Posted by Mario F.
    Code:
    void initialize(int& words, int& lines, int& paragraphs, char& letter, int& totalWords)
    This is completely unecessary. It's a big ugh! Just initialize the variables at the point of declaration. You will thank yourself later when rereading your code.
    Yeah, I know that. This is a book problem assigned by my teacher, and it says to create a function to initialize all the variables. Guess its practice for creating functions? Dunno.

    Quote Originally Posted by Mario F.
    Code:
    	letter = input.get();
    		
    	while (!input.eof())
    	{          
              while (letter != '\n')
    look closely at that part of your code... what do you think happens on the while loop?
    Well the program executes, and never terminates. Its like an infinite loop, but nothing happens. First while loop makes sure the its not the end of the file, then the second loop makes sure its not a new line. Makes sense to me? When it is a new line, it just skips over the...oh wait. I think I see what you're getting at.


    EDIT: Nope, dont see what you're talking about. Looks good to me, haha. Problem with the '\n' ?

    EDIT2: Wait, its never going to leave that loop, is it. That makes complete sense.
    Last edited by tortan; 09-19-2006 at 08:26 PM.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Yessss. Thanks so much dude. I cant believe I looked over that.


    Thanks again.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I didn't say anything
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by tortan
    I wrote this program that takes a text file, inputs it one character at a time. The program counts the number of lines, words, and paragraphs, then outputs them.
    Code:
    void updateCount(int& words, int& lines, int& paragraphs, int& totalWords)
    {
         if (words = 0)
         {
             paragraphs++;
         }
         
         words += totalWords;
         
         if (words != 0)
         {
             lines++;
         }
         words = 0;
    }
    You probably didn't mean that assignment rather that test for equality.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM