while (cin) loop problems

This is a discussion on while (cin) loop problems within the C++ Programming forums, part of the General Programming Boards category; It has been a while since I have done any C++ programming so I am a little rusty. I am ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    while (cin) loop problems

    It has been a while since I have done any C++ programming so I am a little rusty. I am trying to do a simple while(cin) loop to read in each word of the users input and then exit back to the main menu. To simplify things, I tried creating a while loop in a new cpp to rule out any other possible problems. heres what I got:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	char string1[64];
    	while (cin >> string1)
    	{
    		cout << string1 << "* ";
    	}
    	cin.clear();
    	cout << " out";
    
    	return 0;
    }
    when I compile the code, the program stays in the while loop; it doesn't hang because it responds to any additional input I give it, but it does not ever output " out" as I would like. Ultimately I would like this program to take an input from the keyboard like "test1 test2" and output "test1* test2* out".

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You have to determine when you want to stop reading data from the keyboard. As it is now, your while loop continues until there is a stream error. A stream error will cause the stream object returned by the operator>>, i.e. cin, to evaluate to false, and terminate the while loop. End-of-file is considered a stream error, but unlike with a file, there's no automatic end of file marker in keyboard input--hitting Return just enters a \n in the input stream, which can be read in or skipped with the >> operator. To produce an eof marker from the keyboard, the user has to hit ctrl Z, or return + ctrl Z, or even return + ctrl Z twice.
    Last edited by 7stud; 11-06-2005 at 04:35 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Re

    Thanks for the quick reply.

    Is there any way to read in and process multiple words from the same line and then return to a menu? More specifically, I wrote a small proggy that searches a dictionary file for a match to a word that the user inputs. I would like to be able to enter multiple words on the same line seperated by spaces and have the proggy reply with whether or not each word is in the dictionary. Currently I can do this, but between words the proggy still displays the "Enter a word:" message. I would like it to reply for each word and then display "Enter a word:" again.

    Heres my current code for this working program:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    ifstream fin;
    char fname[32];
    char string1[64];
    char string2[64];
    int result;
    int result2;
    int search();
    
    int main(int argc, char *argv[])
    {
    	while (1==1)
    	{
    		cout << "Enter the word:";
    		search();
    	}
    	
    	return 0;
    }
    
    int search()
    {
    	int match = 0;
    
    		cin >> string1;
    		fin.open("dictionary.txt", ios::in);
    		while( !fin.eof() ) 
    		{
    		
    			fin >> string2;
    			result = _stricmp( string1, string2 );
    			if (result == 0)
    			{
    				match = 1;
    				cout << string1 << " is a valid word\n\n";
    			}
    				
    
    		}
    		if (match != 1)
    		{
    			cout << string1 << " is not a valid word\n\n";
    		}
    		fin.close();
    		fin.clear();
    		return 0;
    
    }
    Last edited by SeXy_Red; 11-06-2005 at 05:13 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You don't need <stdio.h>, and if you did, in C++ it should be <cstdio>. (You do need <cstring>, though, for _stricmp() (which is non-standard; you must be using MSVC).)

    Code:
    while (1==1)
    There's no need to use 1==1. You can use 1. (Or for( ; ; ).)

    As for your problem, you could use getline() and then separate the string into tokens.
    Last edited by dwks; 11-06-2005 at 02:27 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I would also add that you should never make an infinte loop like that.

    Consider reasons why you might want to leave that loop. What if your input stream fails? Can you think of a way to say, "If cin fails, exit the loop?"
    Sent from my iPadŽ

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Besides exit().

    And I don't think you should call clear() on a close()ed stream.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in opening a txt file in a loop
    By anilk in forum C Programming
    Replies: 1
    Last Post: 02-05-2006, 02:35 AM
  2. cin infinite loop
    By progbass8 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2005, 03:50 PM
  3. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 12:58 AM
  5. cin.getline and cin problems in a while loop
    By UnclePunker in forum C++ Programming
    Replies: 12
    Last Post: 05-07-2002, 09:43 AM

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