Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   getline()'s and loops (http://www.programmingforums.org/showthread.php?t=15422)

ShawnStovall Mar 15th, 2008 8:28 PM

getline()'s and loops
 
I'm building a simple program to translate a sentence typed in into l33t. (I know, it's stupid, but I thought it would be a good programming exercise for me.) I have hit a road block of sorts. I have a loop that contains the main part of the program, and in that loop I have a getline()(string:: I think?) to get typed text into a string object. Well, the program works perfectly until the client wants to run the loop again. On the second time around it seems that the getline() doesn't except the input and skips directly to the part where it asks the user if he/she wants to go through the process again. Here is my code and help would be appreciated. Thanks.

:

  1. // main.cpp -- this will contail the main body of the new leetspeek program
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include "LEETlib.h"
  6.  
  7. using std::cout;
  8. using std::endl;
  9. using std::cin;
  10. using std::string;
  11. using LEETlib::leetio;
  12.  
  13. int main()
  14. {
  15.         int skp = 0;
  16.         char again = 'y';
  17.  
  18.         while(again != 'n')
  19.         {
  20.                 string eng;
  21.  
  22.                 if(skp == 0)
  23.                 {
  24.                         cout << "Programed by Shawn Stovall\n03-14-2008\nv0.8\n\n";
  25.                         skp += 1;
  26.                 }
  27.  
  28.                 cout << "Please type in a line you would like to change into |_337: ";
  29.                 getline(cin, eng);
  30.  
  31.                 leetio(eng);
  32.  
  33.                 cout << "\nWould you like to translate another group of words? (y/n) ";
  34.                 cin >> again;
  35.  
  36.         }
  37.  
  38.         cout << "\nGoodbye!\n\n";
  39.  
  40.         system("PAUSE");
  41.         return 0;
  42. }


Ooble Mar 15th, 2008 10:17 PM

Re: getline()'s and loops
 
I'm guessing there's still a newline in the buffer, so getline is picking it up each time it loops through. You need to read it and discard it.

:

getline(cin, eng);
cin.get();


ShawnStovall Mar 15th, 2008 10:25 PM

Re: getline()'s and loops
 
Thanks, it took some strategic placing, but it did the trick! :)


All times are GMT -5. The time now is 3:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC