Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2008, 8:28 PM   #1
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 25
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
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.

C++ Syntax (Toggle Plain Text)
  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. }
ShawnStovall is offline   Reply With Quote
Old Mar 15th, 2008, 10:17 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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();
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 15th, 2008, 10:25 PM   #3
ShawnStovall
Call me Chuck
 
ShawnStovall's Avatar
 
Join Date: Aug 2007
Location: USA, MI
Posts: 25
Rep Power: 0 ShawnStovall is on a distinguished road
Send a message via AIM to ShawnStovall
Re: getline()'s and loops

Thanks, it took some strategic placing, but it did the trick!
ShawnStovall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:23 PM.

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