need some help :)

This is a discussion on need some help :) within the C++ Programming forums, part of the General Programming Boards category; basically i have to write a program that looks like what is below and the goal is for the program ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    need some help :)

    basically i have to write a program that looks like what is below and the goal is for the program to output whats in the file and how many words, lines, paragraphs there are (blank lines between paragraphs).

    i understand how to use the .eof() function to read the file till it reaches the end but what is used to find the end of the line? i think once i have that little piece of code i'll be fine doing the rest.

    Code:
    int main()
    {
         variable declaration
         open files
    
         read a characeter
    
         while(not end of file)
        {
             while(not end of line)
             {
                 processblank(); // some function
                 copytext(); // some function
             }
    updatecount(); // some function
    read a character;
    .
    .
    .
    }
    
    print total(); // some function
    close files;
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> but what is used to find the end of the line?
    The character '\n' is a newline and signifies the end of a line.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    i've tried
    Code:
     
    int main ()
    {
    	ifstream infile;
    	char ch;
    	infile.open("textfile.txt");
    	
    	while (!infile.eof())
    	{
    		
    		while(ch != '\n')
    		{
    		infile >> ch;
    		cout << ch;	
    		}
    
    	}
    
    
    	infile.close();
    	return 0;
    }
    but get an inf loop. just trying to get it to read the lines atm not really worried about the rest of the program

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    When you read a character with operator>>, it skips whitespace like spaces, tabs and newlines. If you are going to follow the pseudocode in your first post, you should probably use get() to get a single character at a time.

    Note that your current code also doesn't implement the pseudocode correctly in terms of eof(). Using eof() to find the end of the file is tricky. The pseudocode does it correctly, but your code fails because it checks eof() just before it reads in a character instead of just after.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    i used the while statement below and it printed out the sentences right.. but once i take the // away from the second while it prints the first setence and moves the cursor to the second line and nothing else happens.. been playing around with this all day trying to make it work and i just cant figure it out

    Code:
     
    while (!infile.eof())
    	{
    		
    		//while(ch != '\n')
    		{
    		infile.get(ch);
    		cout << ch;	
    		}
    
    	}

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Follow what happens in your code. You check for eof(), it is not the end of the file, so you enter the first loop. Then you check to see if ch is '\n'. It is not, so you enter the second loop. You get a new value into ch with infile.get(ch). You output that value. Then you continue to the start of the second loop again where you check to see if the character is a newline. It isn't so you enter the second loop again.

    This continues until you read in the newline character with infile.get(ch). You output that newline character (which puts the cursor on the next line), and then you move back to the start of the second loop again. This time, ch is equal to '\n', so you break the loop and go back to the first loop. The eof() still hasn't been reached because there are still characters on other lines, so you continue into the first loop. That brings you back to the second loop again. But ch is still the '\n' from the last line, so the second while loop never gets entered. It goes back to the first while loop. This keeps going back and forth forever because ch will never change from '\n' and the eof() will of course never be false.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > while (!infile.eof())
    See the FAQ for why this is a bad idea.

    Code:
    while ( infile.get(ch) ) {
        cout << ch;	
    }
    Should just dump the file to the screen.
    You can then add any
    if ( ch == )
    type logic you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> See the FAQ for why this is a bad idea.
    Note that the issue discussed in the FAQ does not occur if the original pseudocode is followed, although changing the code to Salem's suggestion is still a good idea for other reasons like the ability to check for errors other than end of file.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > while(not end of file)
    It doesn't?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    If you read, then check for end-of-file, then use the information read, you won't have that problem. The original pseudo-code does this by reading before the loop, then checking end-of-file in the loop control, then using the character inside the loop, and finally reading the next character at the end of the loop.

    The problem in the FAQ is when you check end-of-file, then read, then use what you've read. According to that FAQ, end-of-file marker is only set after the read attempt fails, so you are using the data from the previous read.

    The situation is actually more complicated than that. When I have tested this with C++, I found, at least on one implementation, that the end-of-file flag is set before you attempt to read past the end of the file. So if you have read and consumed the last character in the file but not attempted to read further, then eof() flag is still true. This means that using get() and getline() in a loop with while (!eof()) is ok, because they will consume the last character in the file and the eof flag will be set. If you read in with operator>>, and the file has a trailing newline, then the problem arises again because the last newline is still in the stream so the eof bit is not set, but the next read will fail because there is no data after it.

    So in some cases, using eof to control a loop will work as intended. However, all of this means that in general it is not a good idea and the suggestion of the FAQ is still the best way to go. Using the return value of the read tells you if the read succeeded or failed. If it succeeded, you use the value and continue the loop. If it failed you break the loop. All these other details don't matter.

Popular pages Recent additions subscribe to a feed

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