Thread: using while(cin)

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    using while(cin)

    help, i've spent many a desperate compile trying to tweak this code to get it to run but can't get it to work. The code looks like this:

    Code:
    char letter;
    
    while(cin)
    {
    cin>>letter;
    // do some stuff
    }
    the purpose of the code is to pull in whatever the user types one character at a time and when they hit enter to process it. The problem is that when I try it and i hit enter it just moves the cursor down one line and waits.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    edit...

    Your're never outputting anything, so it's waiting for input like you told it to do so.
    Code:
    char letter;
    
    while(cin && letter != 'q')
    {
         cout<<"Enter a character: ";
         cin>>letter;
         cout<<"You entered "<<letter<<endl;
    }
    Last edited by funkydude9; 08-15-2003 at 05:04 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    I understand that, but is there a way to let the person type more then one character, say a sentence and then when they hit enter to let me process it one character at a time? I've thought of using the getch() function in conio.h but I've run into problem that if they make a mistake and need to hit backspace. I want to give them the freedom to type freely until they want to stop, at which time they press enter.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    char* myStr = new char;
    cin>>myStr;
    cout<<myStr;
    }
    And if you need to edit it character by character you can just loop through the array or something:
    Code:
    for (int i=0;myStr[i]!='\0';i++)
     //do something with myStr[i]
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can read in the whole sentence using cin.getline(), and then process the variable containing the sentence char by char.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by JaWiB
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    char* myStr = new char;
    cin>>myStr;
    cout<<myStr;
    }
    *cough*Free Memory*cough*

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    ERg always forget...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    *cough* You also allocate only one byte for the buffer *cough*

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    *cough* Forgot to return 0 *cough*
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    *cough* Not to mention it won't work if the user enters multiple words. *cough*

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    *cough* Forgot to return 0 *cough*
    Nope don't have to

    *cough* Ok that's enough *cough*
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    The cin works the same way as a file stream. That is to say, the expression cin >> ??? would return true until you have pressed Ctrl-Z (EOF) and Enter.

  13. #13
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    ummm...

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <conio.h>
    
    using namespace std;
    
    int main(std::string argv , int argc) {
    
    string yourName = "doodlepoodle";
    
    while( (getch())  && (getch() != 'q') ) //get input. input shouldn't be 'q' or else quits. 
     {
     cout << "hello "  << yourName << "\n"; // prints hello "yourName".
     } 												 // but dont forget to
     system("pause");			    				// press "enter" in 
     }		        									// the console 
    	
    //The problem with this code is however that it asks for input two times.
    // i.e to quit i have to press 'q' as last one .
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Verifying int with while(!(cin >> num))
    By motarded in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2006, 10:37 PM