cin.getline() question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

cin.getline() question

 
0
  #1
May 12th, 2009
Well, once again, hello. I'm trying to figure out why when i use 2 cin.getlines after eachother it skips one of them and I cant use it. Take a look if you will:

  1. void Join()
  2. {
  3. char* Username = new char[255];
  4. char* Password = new char[255];
  5.  
  6. ofstream DataBase("DataBase.txt", ios::app);
  7. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  8. cout << "Username: ";
  9. cin.getline(Username, 255);
  10. DataBase << Username << "\n";
  11. cout << "\nPassword: ";
  12. cin.getline(Password, 255);
  13. DataBase << Password << "\n\n";
  14. cout << "\nSaved To Database!\n";
  15. DataBase.close();
  16.  
  17. delete [] Username;
  18. delete [] Password;
  19. }

for some reason when I run the program and goto this function it will skip the first cin.getline and only let me type for the cin.getline that gets the Password. It's wierd because I ran a simple program similar to it, and it works fine:

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. using namespace std;
  5.  
  6. int main (int argc, char* argv[]) {
  7. char* string1 = new char[255];
  8. char* string2 = new char[255];
  9.  
  10. cout << "String1: ";
  11. cin.getline(string1, 255);
  12.  
  13. cout << "String2: ";
  14. cin.getline(string2, 255);
  15.  
  16. cout << "\nString1 = " << string1 << endl;
  17. cout << "String2 = " << string2 << endl;
  18.  
  19. delete[] string1;
  20. delete[] string2;
  21.  
  22. getch();
  23. return 0;
  24. }

This code doesnt skip any cin.getline()'s for some reason, could it not work in the other one because of the file i/o? Maybe its making it act wierd? Any help is appreciated, thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cin.getline() question

 
0
  #2
May 12th, 2009
Before the call to Join(), is there another function that leaves unread input (such as a trailing newline) in cin ?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

Re: cin.getline() question

 
0
  #3
May 12th, 2009
I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. char* Username = new char[255];
  8. char* Password = new char[255];
  9. char* User = new char[255];
  10.  
  11. void Join()
  12. {
  13. ofstream DataBase("DataBase.txt", ios::app);
  14. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  15. cout << "Username: ";
  16. cin.getline(Username, 255);
  17. DataBase << Username << "\n";
  18. cout << "\nPassword: ";
  19. cin.getline(Password, 255);
  20. DataBase << Password << "\n\n";
  21. cout << "\nSaved To Database!\n";
  22. DataBase.close();
  23.  
  24. delete [] Username;
  25. delete [] Password;
  26. }
  27.  
  28. void Login() {
  29.  
  30. ifstream DataBase("DataBase.txt");
  31. cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
  32. cout << "Username: ";
  33. cin >> User;
  34. DataBase >> Username;
  35. if (Username==User) cout << "\nUsername Correct!";
  36. else cout << "\nUsername Incorrect!";
  37.  
  38.  
  39.  
  40. delete [] User;
  41. delete [] Username;
  42. delete [] Password;
  43. }
  44.  
  45. void About() {
  46. }
  47.  
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51.  
  52. int option;
  53.  
  54. cout << "DataBase v1.00 BETA!\n\n";
  55. cout << "1.Login\n";
  56. cout << "2.Sign-Up\n";
  57. cout << "3.About\n";
  58. cout << "4.Exit\n\n";
  59. cout << "Option(1-4): ";
  60. cin >> option;
  61.  
  62. switch (option) {
  63. case 1:
  64. Login();
  65. break;
  66. case 2:
  67. Join();
  68. break;
  69. case 3:
  70. About();
  71. break;
  72. case 4:
  73. break;
  74. default:
  75. cout << "Not An Option!";
  76. break;
  77. }
  78.  
  79.  
  80. system("PAUSE");
  81. return EXIT_SUCCESS;
  82. }

Thanks for replying.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cin.getline() question

 
0
  #4
May 13th, 2009
How about in you use Login() where you use cin instead of getline?
  1. cin >> User;
Last edited by Dave Sinkula; May 13th, 2009 at 12:11 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: cin.getline() question

 
0
  #5
May 13th, 2009
You'll have to use cin.ignore() to clean the input buffer, read this thread
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 52
Reputation: Bhoot is an unknown quantity at this point 
Solved Threads: 1
Bhoot's Avatar
Bhoot Bhoot is offline Offline
Junior Poster in Training

Re: cin.getline() question

 
0
  #6
May 13th, 2009
Originally Posted by Deme View Post
I don't think so, I just use a switch/case which then calls it and thats all, heres the complete code:

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. char* Username = new char[255];
  8. char* Password = new char[255];
  9. char* User = new char[255];
  10.  
  11. void Join()
  12. {
  13. ofstream DataBase("DataBase.txt", ios::app);
  14. cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
  15. cout << "Username: ";
  16. cin.getline(Username, 255);
  17. DataBase << Username << "\n";
  18. cout << "\nPassword: ";
  19. cin.getline(Password, 255);
  20. DataBase << Password << "\n\n";
  21. cout << "\nSaved To Database!\n";
  22. DataBase.close();
  23.  
  24. delete [] Username;
  25. delete [] Password;
  26. }
  27.  
  28. void Login() {
  29.  
  30. ifstream DataBase("DataBase.txt");
  31. cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
  32. cout << "Username: ";
  33. cin >> User;
  34. DataBase >> Username;
  35. if (Username==User) cout << "\nUsername Correct!";
  36. else cout << "\nUsername Incorrect!";
  37.  
  38.  
  39.  
  40. delete [] User;
  41. delete [] Username;
  42. delete [] Password;
  43. }
  44.  
  45. void About() {
  46. }
  47.  
  48.  
  49. int main(int argc, char *argv[])
  50. {
  51.  
  52. int option;
  53.  
  54. cout << "DataBase v1.00 BETA!\n\n";
  55. cout << "1.Login\n";
  56. cout << "2.Sign-Up\n";
  57. cout << "3.About\n";
  58. cout << "4.Exit\n\n";
  59. cout << "Option(1-4): ";
  60. cin >> option;
  61.  
  62. switch (option) {
  63. case 1:
  64. Login();
  65. break;
  66. case 2:
  67. Join();
  68. break;
  69. case 3:
  70. About();
  71. break;
  72. case 4:
  73. break;
  74. default:
  75. cout << "Not An Option!";
  76. break;
  77. }
  78.  
  79.  
  80. system("PAUSE");
  81. return EXIT_SUCCESS;
  82. }

Thanks for replying.
You could be facing the problem at either of these lines :
  1. cin >> User;
  1. cin >> option;

Most probably, they would leave a newline behind, in the input stream. Hence, the first getline() used after cin would just 'eat' that newline, and wont take any input.

You should better keep habit of using cin.ignore() to flush the input stream before using getline ( or even cin; i had faced this problem in that too..tho not so sure )

  1. cin.ignore();
  2. cin.getline();

cin.ignore() also has overloaded versions. You should check them out.
Bhoot
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

Re: cin.getline() question

 
0
  #7
May 13th, 2009
Thanks i'll try cin.ignore(), and by the way, i don't think i'd want to just use cin, because when i do it doesnt allow spaces...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 8
Reputation: Deme is an unknown quantity at this point 
Solved Threads: 0
Deme Deme is offline Offline
Newbie Poster

Re: cin.getline() question

 
0
  #8
May 13th, 2009
Thanks guys, it worked like a charm.. ^_^
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC