stays in loop, but it's not an infinite loop (C++)

This is a discussion on stays in loop, but it's not an infinite loop (C++) within the C++ Programming forums, part of the General Programming Boards category; I learned C two or so years ago, and I recently (last year) decided to teach myself cxx. I never ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    stays in loop, but it's not an infinite loop (C++)

    I learned C two or so years ago, and I recently (last year) decided to teach myself cxx. I never got around to doing it until now. So I have this book called C++ Primer 2nd Edition by Stanley B. Lippman. Exercise 0-3 tells me to write a program that will count the amount of blanks, tabs, newlines, and characters (without white spaces) of an input. Few pages earlier it gave a similar program:
    Code:
    /*
    *This program reads a character at a time from standard input
    *until end-of file is encountered.  It keeps a count of both the number of
    *characters and the number of lines it reads.  Its output is of the following form:
    *	lineCount characterCount
    #include <iostream.h>
    
    int main ()
    {
    	char ch;
    	int lineCnt=0, charCnt=0;
    	
    	while(cin.get(ch))
    	{
      switch(ch)
      {
      	case '\t':
      	case ' ':
        break;
      	case '\n':
        ++lineCnt;
        break;
      	default:
        ++charCnt;
        break;
      }
    	}
    	cout<<lineCnt<<" "<<charCnt<<endl;
    	return 0;
    }
    So I based my program off of that (hey, if it's given, I'll take it):
    Code:
    #include <iostream.h>
    
    int main ()
    {
    	char ch;
    	int lineCnt=0, charCnt=0, tabCnt=0, blankCnt=0;
    	
    	while(cin.get(ch))
      switch(ch)
      {
      	case '\t':
        ++tabCnt;
        break;
      	case ' ':
        ++blankCnt;
        break;
      	case '\n':
        ++lineCnt;
        break;
      	default:
        ++charCnt;
        break;
      }
    	cout<<"\t\tTotal Characters: "<<charCnt<<"\n\n\nLines:   "<<lineCnt<<"\nChars:   "<<charCnt<<"\nTabs:    "<<tabCnt<<"\nBlanks: "<<blankCnt<<endl;
    	return 0;
    }
    The output is as follows(I typed in "hello"):
    Code:
    EXECUTING:
    /home/[username removed]/C++/Exercis0.3/Count
    --------------------------------------------------------------
    hello
    And it just stays there... I asked my friend (been cxx programming for ~2.5 years now), and his initial thought was that it was in an infinite loop. I saw it as a possibility, but I didn't see why it would be stuck in an infinite loop, so I ran a test:
    Code:
    #include <iostream.h>
    
    int main ()
    {
    	char ch;
    	int lineCnt=0, charCnt=0, tabCnt=0, blankCnt=0;
    	
    	while(cin.get(ch))
    	{
      switch(ch)
      {
      	case '\t':
        ++tabCnt;
        cout<<"t";
        break;
      	case ' ':
        ++blankCnt;
        cout<<"b";
        break;
      	case '\n':
        ++lineCnt;
        cout<<"n";
        break;
      	default:
        ++charCnt;
        cout<<"d";
        break;
      }
      cout<<" nothing\n";
    	}
    	cout<<"reached\n";
    	cout<<"\t\tTotal Characters: "<<charCnt<<"\n\n\nLines:   "<<lineCnt<<"\nChars:   "<<charCnt<<"\nTabs:    "<<tabCnt<<"\nBlanks: "<<blankCnt<<endl;
    	return 0;
    }
    The output is as follows (I typed in "hello"):
    Code:
    EXECUTING:
    /home/[username removed]/C++/Exercis0.3/Count
    --------------------------------------------------------------
    hello
    d nothing
    d nothing
    d nothing
    d nothing
    d nothing
    n nothing
    As you can see it never leaves the while loop, but it's not an infinite loop. What is going on? Just tell me that, and I might be able to solve the problem on my own.

  2. #2
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    ctrl+d(EOF under unix) or ctrl+z(EOF under windows) will escape the loop
    and primer 2nd is too old
    try primer 4th or 3rd
    Last edited by Antigloss; 07-19-2005 at 06:25 AM.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Yeah... I have 3rd edition, but my mom suggested 2nd edition since it went more into depth in a particular subject, I forget what it was... Anyway, she helped me out (she knows like 12 or so programming languages very well).

    I'm not going to revert to the Ctrl+c or Ctrl+d thing though. I have a different solution that is not so dependent on the OS.

    ----------------------------------Edited----------------------------------
    Plus, that wouldn't allow it to escape the loop. It would terminate the program and not let it finish its task.

  4. #4
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Wait your mom suggested it to you?! Oh snap i wish my mom new how to program.

    Oh and howcome you just don't ask her to help you out?

    -----------------------------------

    It never leaves the while loop because the condition you provided stays true.

    When you use something something like Ctrl+Z, that puts the error flag in the input stream you're reading from (in this case cin) which will invalidate your condition and exit the loop.

    Also, using Ctrl+z won't terminate your program, just exit your loop.

    ...i think.
    Last edited by dra; 07-19-2005 at 12:57 AM.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Cuz she wasn't here... and I try to fix these things as soon as possible so I can move on

    Ctrl+z does terminate the program (I typed in "hello"):
    Code:
    $ ./C++/Exercise0.3/Count
    hello
    d nothing
    d nothing
    d nothing
    d nothing
    d nothing
    n nothing
    
    [1]+   Stopped                   ./C++/Exercise0.3/Count
    $exit
    [quote=dra]It never leaves the while loop because the condition you provided stays true.[/code]Well... It never even goes into the another iteration because it's still expecting something to be read in by the cin.get(ch). So it actually doesn't stay true (otherwise it would keep repeating "nothing\n"), it's just never able to be evaluated. I just need something to evaluate it to false.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    If you add a cin.ignore(); before the return 0; you can send a ctrl+Z / ctrl+D and the program will not terminate (the loop will break).

    it looks like the original intention was to redirect a file to the program as stdin.

    Code:
    /countprogram.exe < file_to_count.txt
    the terminating condition of the while loop is EOF, as stated by the comments of the original program, and by antigloss



    I just need something to evaluate it to false
    So what do you want that to be?
    It seems like you want it to be a newline (which would make counting newlines kind of pointless).
    so just add something like
    Code:
    && ch != '\n'
    to your while condition
    Last edited by spydoor; 07-19-2005 at 10:26 AM.

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    use ctrl+d. you're using unix, right?

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    yeah, I'm using Linux. ........guys, I swear I said I solved the problem...

    Quote Originally Posted by Berticus
    I'm not going to revert to the Ctrl+c or Ctrl+d thing though. I have a different solution that is not so dependent on the OS.

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Your code itself said,
    Code:
    This program reads a character at a time from standard input
    until end-of file is encountered.
    I don't see the problem when people recommend actually typing the end-of-file character, which is Ctrl+D on Unix-like systems, Ctrl+Z on Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Replies: 6
    Last Post: 10-23-2006, 08:22 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. infinite loop
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-22-2001, 12:04 PM

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